PHP Switch


The switch allows selection of multiple possible outcomes for a scalar(expression).

grammar:

switch(expr) {
     case result1:
     Statement1
     break;
     case result2:
     Statement2
     break;
     ...
     default:
     Statement
}

The system evaluates the value of expr and selects the corresponding statement based on the result(result1, result2, etc.). If all the case results do not match, the default statement is executed.

example:

switch($x) {
     case 0:
     echo " x equals 0 ";
     break;
     case 1:
     echo " x equals 1 ";
     break;
     case 2:
     echo " x equals 2 ";
     break;
     Default:
     echo " x is not equal to 1 and 2, nor equal to 0 ";
}

Tip

  • You can have multiple case conditions to judge
  • The results behind the case are not limited to numbers, but can also be characters or other types of PHP support
  • default is not required



Copyright 2014-2017 by DeveloperTut.com. All Rights Reserved.
TwitterGoogle+Facebook