PHP Switch
The PHP switch is another conditional statement which carries out multiple condition check and execute the appropriate task based on the condition testing.
The PHP switch statement is a decent replacement of PHP if-elseif-else structure.In switch case structure only exact values can be compared i.e greater than or less than comparison is not present.
Example:
<?php
switch ($input)
{
case 1:
echo "Your entered 1";
break;
case 2:
echo "You entered 2";
break;
case 3:
echo "You entered 3";
break;
default:
echo "Your value is not within the range";
}
?>
switch ($input)
{
case 1:
echo "Your entered 1";
break;
case 2:
echo "You entered 2";
break;
case 3:
echo "You entered 3";
break;
default:
echo "Your value is not within the range";
}
?>
The code above is the basic syntax of a switch...case statement in PHP.The break;is used here to terminate the program when any condition is true.If no condition is true.If no condition is matched, the code in the default: code block is executed.
0 comments:
Subscribe to:
Post Comments (Atom)
Post a Comment