PHP switch statement
Another useful php statement that is used to execute different actions based on different conditions.
Syntax:
Example:
switch statement evaluates one time the variable, the value then is compared each case, if it match the block of code is executed if not the switch statement executes the default code if the programmer set it.
Another useful php statement that is used to execute different actions based on different conditions.
Syntax:
switch (variable)
{
case conditon1:
//code to be executed
break;
case condition2:
//code to be executed
break;
default:
//code to be executed
}
Example:
<html>
<body>
<?php
$variable1 = 2;
switch ($variable1)
{
case 1:
echo "Number 1";
break;
case 2:
echo "Number 2";
break;
case 3:
echo "Number 3";
break;
default:
echo "No number between 1 and 3";
}
?>
</body>
</html>
switch statement evaluates one time the variable, the value then is compared each case, if it match the block of code is executed if not the switch statement executes the default code if the programmer set it.
No comments:
Post a Comment