Here based on the weekday we will display a message. Let us use switch function in our weekday's condition. Here is the code for handing string variable with switch function. 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. We can use switch function for a string variable. However the script below will not work for value $j=nullĪbove code will fail if $j=null, for all other values it is fine. Switch() with OR, matching multiple cases Ībove code will work even if $j=null ( No matches ). Please read theĪbove examples of PHP switch statement and use in your code. The last case or known as default case will be executed here. $j=5 /// we assigned a value to the variable hereĮcho "value of $j = 0 // This line will not be executedĮcho "value of $j = 1 // This line will not be executedĮcho "value of $j = 2 // This line will not be executedĮcho "value of $j = 3 // This line will not beexecutedĮcho "value of $j = 4 // This line will not be executedĮcho "value of $j = Default value " // This line will be executed It will not match with any of the listed cases or conditions. We will set the value of our variable to 5 so Some time we may specify some condition which should be executed if all otherĬonditions are not matched. $j=2 /// assigned a value to the variable hereĮcho "value of \$j = 0" // This line will not be executedĮcho "value of \$j = 1" // This line will not be executedĮcho "value of \$j = 2" // This line will be executedĮcho "value of \$j = 3" // This line will not be executedĮcho "value of \$j = 4" // This line will not be executed Have to use break statement after case inside switch function. Not required and we want to stop the execution of other codes after it then we Here the php code execution will not stop after matching the caseĢ and goes on evaluating the code below it also. You can see in the above case whenever the case is satisfied all theĬodes below it gets executed even though the cases are different than the main $j=2 /// we assigned a value to the variable hereĮcho " value of \$j = 0 " // This line will not be executedĮcho " value of \$j = 1" // This line will not be executedĮcho " value of \$j = 2" // This line will be executedĮcho " value of \$j = 3" // This line will be executedĮcho " value of \$j = 4" // This line will be executed We can tell a default condition also if all other conditions fails to match. Within it depending on the use of break statement. Statement and its cases and once the code is valid then it will execute the code The PHP code will evaluate the PHP switch See the demo script at the end of this page. Here we know it is always one particular day will match, excluding all other possibilities. One common example is finding out the day of the week and display a message based on the particular day. (Repeated checking by if condition can be avoided) We can use several PHP ifĬonditions with php if else but this is a better way of coding when we know one Options are to be executed based on some conditions. PHP switch statement is used when one of the several options or a groups of $j='Cricket' /// we assigned a value to the variable hereĮcho " You play Football " // This line will not be executedĮcho " You play Cricket " // This line will be executedĮcho " You play Hockey " // This line will not be executed
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |