0

I am trying to make some logic controls on input variables $ContentPicture1Title and $ContentPicture1URL. In particular I am trying to get two rules applied in a single if statement.

In the first piece of code was everything ok. Then I messed out the code as you can see in the second snippet.

First statement (OK):

<?php if (is_null($ContentPicture1Title)){ ?>
   <div class="letter-badge"><img src="image1.jpg"></div>
<?php } else { ?>
   <div class="letter-badge"><img src="image2.jpg"></div>
<?php }?>

Second (messed) statement:

<?php if (is_null($ContentPicture1Title) && ($ContentPicture1URL)){ ?>
   <div class="letter-badge"><img src="image1.jpg"></div>
<?php } else { ?>
   <div class="letter-badge"><img src="image2.jpg"></div>
<?php }?>

Thank you in advance for your help.

3
  • 1
    And what isn't working? What errors are you getting? Commented Jun 21, 2016 at 12:04
  • 1
    I guess your second is missing the is_null(...). But your question is not very clear! Commented Jun 21, 2016 at 12:04
  • yes my server does not give me correct errors ! both fields are being checked for null value.. in other words empty. Commented Jun 21, 2016 at 12:19

2 Answers 2

1

Unless $ContentPicture1URL is a boolean variable, you need to invoke a function that can be evaluated as a boolean and/or compare it to another variable/value.

e.g.

if(is_null($variable1) && is_null($variable2)) {
    //Do something
}

or

if(is_null($variable1) && $variable2 == 5) {
    //Do something
}
Sign up to request clarification or add additional context in comments.

2 Comments

PHP will cast the variable as boolean and return a true or false from it. Not exactly sure what are the rules, but it does. No need to be exactly a boolean
True, but the variable is evaluated as a boolean either way. (e.g. 0 == false and 1 == true). It's unlikely he would want this unless it was a numeric or boolean value.
0

As you can read in official documentation you need to use the second construct:

<?php if (is_null($ContentPicture1Title) && $ContentPicture1URL): ?>

   <div class="letter-badge"><img src="image1.jpg"></div>

<?php else: ?>

   <div class="letter-badge"><img src="image2.jpg"></div>

<?php endif; ?>

This will run if $ContentPicture1URL is a boolean, otherwise you need to use compare operators ( ==, !=, &&, ||, etc.) or boolean functions (is_null()) in order to verify the condition properly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.