0

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?

cp11
  • 25
  • 2
  • 5

3 Answers3

1

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.

phant0m
  • 16,595
  • 5
  • 50
  • 82
0
if ($city == NULL && $state == NULL)

Logical Connective

The && means AND || would be OR

arminb
  • 2,036
  • 3
  • 24
  • 43
0

<?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.

Community
  • 1
  • 1
Jay
  • 2,141
  • 6
  • 26
  • 37