PHP SWITCH STATEMENT
The switch statement in PHP is used to perform one of several different actions based on one of several different conditions.
Switch Statement:
In general, the users want to execute the code one of many blocks of code to be executed, use the switch statement.
The switch statement is used to avoid long blocks of if..elseif..else code.
Syntax:
switch(expressions)
{
case condition 1:
code to be executed if expression = condition 1;
break:
case condition 2:
code to be executed if expression = condition 2;
break:
default:
code to be executed
Note: If first expression and the second expression is different
Then we will see this is how it works
1. A single expression is evaluated once.
2. The value of the expression is compared with the values for each in the structure.
3.If there is a match, the code associated with that case is executed.
4.after a code is executed break is used to stop the code from running into the next case.
5.the default statement is used in none of these cases are true.
Example:
<html>
<body>
<?php
favfruit ="banana"
switch($favfruit)
{
case "banana":
echo "My favorite fruit is banana!";
break;
case "apple":
echo "My favorite fruit is apple!";
break;
case "orange":
echo "My favorite fruitis orange!";
break;
}
?>
</body>
</html>
Output:
My favorite fruit is banana!
No comments:
Post a Comment