SQL PHP HTML ASP JavaScript articles and free scripts to download
 
 

PHP switch statement script and example



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.

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
<br> ";       // This line will not be executed

case 1:
echo "value of $ j = 1
<br> ";       // This line will not be executed

case 2:
echo "value of $ j = 2
<br> ";       // This line will be executed

case 3:
echo "value of $ j = 3
<br> ";        // This line will be executed

case 4:
echo "value of $ j = 4
<br> ";       // This line will be executed
}


Related Tutorial
PHP for loop
While loop
PHP file upload
PHP Signup script
 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; /// we assigned a value to the variable here

switch ($j)
{
case 0:
echo "value of $ j = 0
<br> ";       // This line will not be executed
break;

case 1:
echo "value of $ j = 1
<br> ";       // This line will not be executed
break;

case 2:
echo "value of $ j = 2
<br> ";       // This line will be executed
break;

case 3:
echo "value of $ j = 3
<br> ";        // This line will not be executed
break;

case 4:
echo "value of $ j = 4
<br> ";       // 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
<br> ";       // This line will not be executed
break;

case 1:
echo "value of $ j = 1
<br> ";       // This line will not be executed
break;

case 2:
echo "value of $ j = 2
<br> ";       // This line will not be executed
break;

case 3:
echo "value of $ j = 3
<br> ";        // This line will not be executed
break;

case 4:
echo "value of $ j = 4
<br> ";       // This line will not be executed
break;

default:
echo "value of $ j = Default value
<br> ";       // 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. 

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 = Friday

Hi today is Friday , Tomorrow your weekend starts

$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";

}


You can read how switch function is used for conditional redirecting users.

Discuss this tutorial at forum


 

Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.

Scripts
PHP
JavaScript
PHP Tutorial Index
Popular Tutorials
Drop down list
File Upload
Signup script
Member Login
Line Graph
PHP MySQL Paging
PHP Calendar
PHP Tutorials
Date & Time
Array
String Functions
Math Functions
Form Handling
File Handling
Comment Posting
Content Management
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.