cp11
cp11

Reputation: 25

YII: Need to combine these two If statements?

I'm using the Yii framework. I want to show specific content if a user did not select their location.

So, I am using the following code to show content if no state is selected:

    <?php if ($state == NULL) { ?>

And I am using the following code if no city is selected:

    <?php if ($city == NULL) { ?>

And they are working perfectly, but how can I combine those two statements so that I can show content if they have no state, AND, no city selected?

Upvotes: 0

Views: 1454

Answers (3)

phant0m
phant0m

Reputation: 16905

There are four different cases:

  • both statements are true
  • the first is true
  • the second is true
  • none are true
if($state == NULL && $city == NULL) {
    // both are true
}
else if($state == NULL) {

}
else if{$city == NULL) {

}
else{
   // neither is true, i.e. both are selected
}

The order in which they are listed is important: You must check for the case that both conditions are true first!

For more information, see the PHP documentation on logical operators: AND, OR, XOR, NOT.

Upvotes: 1

Jay
Jay

Reputation: 2141

<?php if ($city == NULL && $state == NULL) { ?> I suggest that you read about operators in PHP. Also worth reading is the PHP symbol reference FAQ of SO.

Upvotes: 0

arminb
arminb

Reputation: 2103

if ($city == NULL && $state == NULL)

Logical Connective

The && means AND || would be OR

Upvotes: 0

Related Questions