$j='Cricket'; /// we assigned a value to the variable here
switch ($j)
{
case 'Football':
echo " You play Football "; // This line will not be executed
break;
case 'Cricket':
echo " You play Cricket "; // This line will be executed
break;
case 'Hockey':
echo " You play Hockey "; // This line will not be executed
break;
}
Output
You play Cricket
PHP switch statement is used when one of the several options or a groups of
options are to be executed based on some conditions. We can use several PHP if
conditions with php if else but this is a better way of coding when we know one
of the several options is correct. (Repeated checking by if condition can be avoided)
$j=2; /// we assigned a value to the variable here
switch ($j)
{
case 0:
echo " value of \$j = 0 "; // This line will not be executed
case 1:
echo " value of \$j = 1"; // This line will not be executed
case 2:
echo " value of \$j = 2"; // This line will be executed
case 3:
echo " value of \$j = 3"; // This line will be executed
case 4:
echo " value of \$j = 4"; // This line will be executed
}
You can see in the above case whenever the case is satisfied all the
codes below it gets executed even though the cases are different than the main
variable. Here the php code execution will not stop after matching the case
2 and goes on evaluating the code below it also. If our case it is
not required and we want to stop the execution of other codes after it then we
have to use break statement after case inside switch function.$j=2; /// assigned a value to the variable here
switch ($j)
{
case 0:
echo "value of \$j = 0"; // This line will not be executed
break;
case 1:
echo "value of \$j = 1"; // This line will not be executed
break;
case 2:
echo "value of \$j = 2"; // This line will be executed
break;
case 3:
echo "value of \$j = 3"; // This line will not be executed
break;
case 4:
echo "value of \$j = 4"; // This line will not be executed
break;
}
Some time we may specify some condition which should be executed if all other
conditions are not matched. We will call it default condition. Here is the way
we will enter a default condition. We will set the value of our variable to 5 so
it will not match with any of the listed cases or conditions.
$j=5; /// we assigned a value to the variable here
switch ($j)
{
case 0:
echo "value of $j = 0; // This line will not be executed
break;
case 1:
echo "value of $j = 1; // This line will not be executed
break;
case 2:
echo "value of $j = 2; // This line will not be executed
break;
case 3:
echo "value of $j = 3; // This line will not beexecuted
break;
case 4:
echo "value of $j = 4; // This line will not be executed
break;
default:
echo "value of $j = Default value " // This line will be executed
break;
}
The last case or known as default case will be executed here. Please read the
above examples of PHP switch statement and use in your code.
<?Php
$j=2;
switch ($j ){
case 1:
case 2:
case 3:
$str = ' equal to 1 or 2 or 3 ';
break;
case 4:
$str = ' equal to 4 ';
break;
case 5:
$str = ' equal to 5 ';
break;
default:
$str = ' No matches ';
break;
}
echo $str;
?>
Output is
equal to 1 or 2 or 3
Above code will work even if $j=null; ( No matches ) . However the script below will not work for value $j=null
<?Php
$j=2;
switch ($j ){
case ($j >=1 && $j <=3 ):
$str = ' equal to 1 or 2 or 3 ';
break;
case 4:
$str = ' equal to 4 ';
break;
default:
$str = ' No matches ';
break;
}
echo $str;
?>
Above code will fail if $j=null, for all other values it is fine.
$position="dba";
switch($position)
{
case "member":
echo "Welcome Member";
break;
case "siteadmin":
echo "Welcome siteadmin";
break;
case "designer":
echo "Welcome Designer";
break;
case "dba":
echo "Welcome dba";
break;
default:
echo "Welcome you all others ";
break;
}
Let us use switch function in our weekday's condition. Here based on the weekday we will display a message. You will get the message based on the server side day of the week.
Day of the Week, Full name = Saturday Hi today is Saturday, Are you still working ?
Here is the code of above script
$tm=time();
//$today=getdate(mktime(0,0,0,2,14,2008));
// Use the above line and comment below line for changing the days and test the script.
$today=getdate($tm);
echo "Day of the Week, Full name = $today[weekday]<br><br>";
switch($today[weekday])
{
case "Sunday":
echo "Hi today is Sunday , Enjoy";
break;
case "Monday":
echo "Hi today is Monday, Good luck for a productive week";
break;
case "Tuesday":
echo "Hi today is Tuesday";
break;
case "Wednesday":
echo "Hi today is Wednesday ";
break;
case "Thursday":
echo "Hi today is Thursday , two more days for a weekend";
break;
case "Friday":
echo "Hi today is Friday , Tomorrow your weekend starts";
break;
case "Saturday":
echo "Hi today is Saturday, Are you still working ?";
break;
default:
echo "Sorry nothing matches , some problem is there";
}
Switch function is better than using nested if-else condition to save server resources and for faster page loading.
You can read how switch function is used for conditional redirecting users.prakash | 28-10-2015 |
send me all code of php using all type of loop |