Switch Statement
The Switch statement in PHP is used to perform one of several different actions based on one of several different conditions.If you want to select one of many blocks of code to be executed, use the Switch statement.
Switch statement is similar to if..else if..else statement.
Switch statement is used to avoid long blocks of if..else if..else code.
Syntax:
switch (expression) {
case label1:
code to be executed if expression = label1;
break;
case label2:
code to be executed if expression = label2;
break;
default:
code to be executed
if expression is different
from both label1 and label2;
}
How it works:
1.The value of the expression is compared with the values for each case in the structure .2.If there is a match, the code associated with that case is executed.
3.After a code is executed, break is used to stop the code from running into the next case.
break means, if there is one statement executes, then there is no need to go rest of case statement.
4.The default statement is used if none of the cases are true.
Last case is always default. This is similar to the last else statement.
If none of the above conditions are true, then default will execute.
Example: 1
<?php
$x = 4;
switch ($x)
{
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";
}
?>
Output
No number between 1 and 3Using Switch to Display Random Quotes:
Example: 2
<?php
//Chooses a random number $num = Rand (1,6);
//Based on the random number, gives a quote
switch ($num)
{
case 1:
echo "I know I can change my life by changing my attitude.";
break;
case 2:
echo "If I can't control my anger, I will be the loser.";
break;
case 3:
echo "I am free from the influence of all negative and
reactive thoughts and words.";
break;
case 4:
echo "Positive thoughts and actions will ensure my wellbeing.";
break;
case 5:
echo "With good planning and initiative I am becomingself
reliant in every respect.";
break;
case 6:
echo "Fortune salutes the brave. I am brave, I am fearless.";
}
?>
Using Switch to Display Random Images:
//Chooses a random number $num = Rand (1,6);
//Based on the random number, gives a quote
switch ($num)
{
case 1:
echo "I know I can change my life by changing my attitude.";
break;
case 2:
echo "If I can't control my anger, I will be the loser.";
break;
case 3:
echo "I am free from the influence of all negative and
reactive thoughts and words.";
break;
case 4:
echo "Positive thoughts and actions will ensure my wellbeing.";
break;
case 5:
echo "With good planning and initiative I am becomingself
reliant in every respect.";
break;
case 6:
echo "Fortune salutes the brave. I am brave, I am fearless.";
}
?>
Output
I am free from the influence of all negative and reactive thoughts and words.Using Switch to Display Random Images:
Example: 3
<?php //Chooses a random number $num = Rand (1,4); //Based on the random number, gives a image switch( $num )
{
case 1: $image_file = "images/1.jpg";
break;
case 2: $image_file = "images/2.jpg";
break;
case 3: $image_file = "images/3.jpg";
break;
case 4: $image_file = "images/4.jpg";
break;
}
echo "Random Image : <img src=$image_file />"; ?>
{
case 1: $image_file = "images/1.jpg";
break;
case 2: $image_file = "images/2.jpg";
break;
case 3: $image_file = "images/3.jpg";
break;
case 4: $image_file = "images/4.jpg";
break;
}
echo "Random Image : <img src=$image_file />"; ?>
Example: 4
<?php
$country = "USA";
switch($country)
{
case "USA":
echo "you are in USA";
break;
case "Japan":
echo "you are in Japan";
break;
default:
echo "you dont know where you are";
break;
} ?>
Output
you are in USA