Cavemanharris
Cavemanharris

Reputation: 183

Multiple If Statements using variable

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.

Upvotes: 0

Views: 89

Answers (2)

Ben M.
Ben M.

Reputation: 311

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
}

Upvotes: 1

morels
morels

Reputation: 2105

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.

Upvotes: 0

Related Questions