Php switch statement

Php switch statements to replace complicated if-elseif statements. Switch statements compare an expression against all of the possible entries inside their body. If they don’t find an exact match, the program will run the “default” clause and ignore the rest of the statement. In PHP, you may use a “break” statement to terminate the code’s execution and pass the control to the succeeding scripts.

Syntax:-


<?php
switch (expression) {
case expression:
statement1
case expression:
statement2
...
default:
statementn
}
?>

Example:-


<?php
$game = "Cricket";
switch ($game) {
  case "Cricket":
    echo "Sachin belongs to cricket!";
    break;
  case "Hockey":
    echo "Dhayan Chand belongs to Hockey";
    break;
  case "Tennis":
    echo "Mahesh Bhupathi belongs to Tennis!";
    break;
  default:
    echo "Players neither belongs to Cricket, Hockey, nor Tennis!";
}
?>
Output:- Sachin belongs to cricket!

Try it Yourself