PHP switch statement script and examplePHP 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.
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.
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.
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
Above code will work even if $j=null; ( No matches ) . However the script below will not work for value $j=null
Above code will fail if $j=null, for all other values it is fine.
switch case for string variableWe 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.
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.
Here is the code of above script
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.
This article is written by plus2net.com team.
| ▼ More on PHP Basics: Loops