PHP Decision Making Statement

PHP

In this tutorial we will learn about PHP decision making statements namely if, else, elseif and switch.

PHP like most other programming languages lets us write conditional statements that helps in making decisions.

if statement

The simplest decision making statement to start with is the if statement.

if ( expression ) {
  //some code
}

//some more code goes here...

In the above code snippet we have created an if statement. The code inside the if block is executed only when the expression evaluated to true. Otherwise it is ignored.

$x = 10;

if ( $x > 0 ) {
  echo "x is greater than 0.";
}

echo " Value of x = " . $x;

In the above code the value of variable $x is 10 which is greater that 0 so, the code inside the if statement is executed.

The above code will print "x is greater than 0. Value of x = 10".

$x = 10;

if ( $x < 0 ) {
  echo "x is less than 0.";
}

echo " Value of x = " . $x;

In the above code $x is not less that 0 so, the code inside the if statement is not executed.

The above code will print "Value of x = 10".

if...else statement

When looking for either-or option i.e., two paths we go for if...else statement.

if ( expression ) {
  //code inside if block
} else {
  //code inside else block
}

//some more code goes here...

We have created an if...else statement in the above code snippet. If the expression of the if statement evaluates to true, then the code inside the if block will be executed. Otherwise, the code inside the else block will be executed.

$x = 10;

if ( $x == 10 ) {
  echo "x is 10";
} else {
  echo "x is not 10";
}

echo "value of x = " . $x;

The above code will print "x is 10" and "value of x = 10".

$x = 5;

if ( $x == 10 ) {
  echo "x is 10";
} else {
  echo "x is not 10";
}

echo "value of x = " . $x;

The above code will print "x is not 10" and "value of x = 10".

elseif statement

If we want multiple options to choose from then we use the elseif statement.

if ( expression1 ) {
  //code inside if block
} elseif ( expression2 ) {
  //code inside 1st elseif block
} else {
  //code inside else block
}

//some more code goes here...

In the above code we have created an elseif statement to have more than 2 options. So, when we run the above code, first the expression of if will be evaluated and if it equals to true then the code inside the if block will be executed.

If it fails, then expression2 of the elseif will be evaluated. And if it is true then code inside the 1st elseif block will be executed.

If the 1st elseif fails then the code inside the else block will be executed.

$x = 10;

if ( $x < 0 ) {
  echo "x is negative.";
} elseif ( $x == 0 ) {
  echo "x is equal to 0";
} else {
  echo "x is positive";
}

echo " value of x = " . $x;

The above code will print "x is positive" and "value of x = 10".

switch statement

We use the switch statement to match a given expression with several different values.

switch ( expression ) {
  
  case 'value1' :
    //some code for value 1
    break;

  case 'value2' :
    //some code for value 2
    break;

  default:
    //some code for default
}

In the above code snippet we have created a switch statement. The expression is matched with the value. Say, for example the expression matches with value2 then, the code for that case block will be executed. If no match is found then the code inside the default block will be executed.

The default block is optional. We use the break keyword to come out of the switch block.

$x = 10;

switch ( $x ) {
  
  case 1:
    echo "x is 1";
    break;

  case 5:
    echo "x is 5";
    break;

  case 10:
    echo "x is 10";
    break;

  default:
    echo "x is something else";

}

echo " value of x = " . $x;

The above code will print "x is 10" and "value of x = 10";

Flow of the above code.

We have set the value of variable x to 10. Next we are passing x as an expression to the switch block. So, the value of x (i.e., 10) is matched with the value of the three case. First, expression 10 is matched with value 1, but it fails. So, 10 is matched with value 5 and it fails too. Next, 10 is matched with value 10 of the third case and it is found to be equal. So, code of the third case is executed and then we break out of the switch block. And then the last line is executed.

Matching multiple case value of switch statement

In the following example the expression is matched with multiple case value.

switch ( expression ) {
  
  case 'value1' :
  case 'value2' :
  case 'value3'
    //some code1
    break;

  case 'value4':
  case 'value5':
    //some code2

  default:
    //some code for default
}

In the above code the expression is first matched with value1, value2 and value3. If any one of them is equal to the expression then code1 is executed and we break out of the switch block.

Otherwise, we match expression with value4 and value5. If match is found then code2 is executed and we break out of the switch block.

If none of the case value match then, we execute the default code block.