PHP - Switch

The switch statement to select one of many blocks of code to be executed.the following syntax and example of switch statement in php.

Syntax


<?php
	switch(expression or variable)
	{
		case label1:
		code to be executed if variable or expression=label1;
		break;
		case label1:
		code to be executed if variable or expression=label2;
		break;
		default;
		code to be executed if variable or expression= is different from both label1 and label2;
		break;
	}
?>						
							

Example


<?php
 
$i = 'apple'; 
switch ($i)
{
    case "apple":
        echo "is apple";
        break;
    case "bar":
        echo "is banana";
        break;
    case "cake":
        echo "is whatermaln";
        break;
}

?>							
							

Output


is apple
							
Share Share on Facebook Share on Twitter Share on Google+ Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit