Switches are not much different from If Else. Both of them describe conditions. The switch statement is used to perform different actions based on different conditions. Please pay attention and practice the explanation below.
The PHP Switch Statement
Use the switch statement to select one of many blocks of code to be executed.
This is the manner by which it works: First we have a solitary articulation n (regularly a variable), that is assessed once. The estimation of the articulation is then contrasted and the qualities for each case in the structure. In the event that there is a match, the square of code related with that case is executed. Use break to keep the code from running into the following case consequently. The default proclamation is utilized if no match is found.
See the example below :
[the_ad id=”651″]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?php $favcolor = "red"; switch ($favcolor) { case "red": echo "Your favorite color is red!"; break; case "blue": echo "Your favorite color is blue!"; break; case "green": echo "Your favorite color is green!"; break; default: echo "Your favorite color is neither red, blue, nor green!"; } ?> |