PHP: Switch Case

Switch case used as an alternative to if-else-elseif to make it simpler to write conditions that are long and repeative.

We use PHP Switch case as follows for the example mentioned earlier:

$sunlight = 0;

switch ($sunlight){
case "0":
echo "sunlight not present";
break;
case "1":
echo "sunlight present";
break;

default:
echo "Not sure about sunlight";
break;
}


This could have done using if-else-elseif also. But, using switch case simplies writing our code and understanding it. Also, note the default case in the switch statement.