PHP switch statement script and example

$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)

One common example is finding out the day of the week and display a message based on the particular day. Here we know it is always one particular day will match, excluding all other possibilities. See the demo script at the end of this page.

The PHP code will evaluate the PHP switch statement and its cases and once the code is valid then it will execute the code within it depending on the use of break statement. We can tell a default condition also if all other conditions fails to match. We will see some examples to see the php switch statement in details.
$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.

switch() with OR , matching multiple cases

<?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

Using AND
<?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.

switch case for string variable

We can use switch function for a string variable. Here we assigned a value to the string variable but in practical cases it can come from user input or from database or from any sources. Here is the code for handing string variable with switch function.
We can use switch function for a string variable. Here we assigned a value to the string variable but in practical cases it can come from user input or from database or from any sources. Here is the code for handing string variable with switch function.
$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 = Monday Hi today is Monday, Good luck for a productive week
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.
PHP Loops PHP While loop For loop if else condition checking
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    prakash

    28-10-2015

    send me all code of php using all type of loop

    Post your comments , suggestion , error , requirements etc here





    PHP video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer