Imogen Johnson
Imogen Johnson

Reputation: 93

PHP if statement, does not validate, only first if runs

I have 3 stages to my if statement. If is front page return true. If is not front page but object is set and has certain value return true. If not return false.

It worked as two seperate statement, 1) if is front page return true if not false 2) if object is set and has given value return true, objects is not set type return false.

When I try to put them as one statement I get true if front page but this rest doesn't return.

Code is below. Is their anything wrong with the statement? Thanks

<?php
$object = get_object();

if(is_front())
{
  return 'true'; 
} 
elseif (!(is_front() & !empty($object))) //is not front but object has been set check value
{
  confirm_value();
}
else (!(is_front() & empty($object))) //if not front and object is not set
{
  return 'false';
}

function confirm_value() {
  $value = load($object); //load object
  if($value->id($id)) //check value of id
  {
    return 'true';  
  } 
  else
  {
    return 'false'; 
  }
}
?>

working code below

<?php
$object = get_object();

if(is_front())
{
  return 'true'; 
} 
elseif (!(is_front() && !empty($object))) //is not front but object has been set check value
{
  return confirm_value();
}
else 
{
  return 'false';
}

function confirm_value() {
  $value = load($object); //load object
  if($value->id($id)) //check value of id
  {
    return 'true';  
  } 
  else
  {
    return 'false'; 
  }
}
?>

Upvotes: 0

Views: 70

Answers (2)

Viet Dinh
Viet Dinh

Reputation: 1961

I don't know the aim of code above but:

  • Use return confirm_value();

  • Else statement do not have condition. If you want check condition you should continue using elseif

  • Should using && instead of &. Because of & is bitwise operator. It will same result with && when all conditions are boolean. But when 1 & 2 == false

Upvotes: 1

Don Dickinson
Don Dickinson

Reputation: 6258

Two problems I see:

1 - you are using a single & when you should be using &&.

2

confirm_value(); 

should be

return confirm_value();

Upvotes: 1

Related Questions