Javascript switch statement is used to execute a block of statement based on the switch expression value. It is like if else if statement. <html> <head> <script> var today; switch (new Date().getDay()) { case 0: today = "Sunday"; break; case 1: today = "Monday"; break; case 2: today = "Tuesday"; break; case 3: today = "Wednesday"; break; case 4: today = "Thursday"; break; case 5: today = "Friday"; break; case 6: today = "Saturday"; } document.write("Today is " + today); </script> </head> <body> </body> </html> (PHP 4, PHP 5, PHP 7, PHP 8) The
In the following example, each code block is equivalent. One uses a series of Example #1
It is important to understand how the
Here, if $i is equal to 0, PHP would execute all of the echo statements! If $i is equal to 1, PHP would execute the last two echo statements. You would get the expected behavior ('i equals 2' would be displayed) only if $i is equal to 2. Thus, it is important not to forget In a The statement list for a case can also be empty, which simply passes control into the statement list for the next case.
A special case is the
If no A case value may be given as an expression. However, that expression will be evaluated on its own and then loosely compared with the switch value. That means it cannot be used for complex evaluations of the switch value. For example:
$target) { For more complex comparisons, the value
true) { The alternative syntax for control structures is supported with switches. For more information, see Alternative syntax for control structures.
It's possible to use a semicolon instead of a colon after a case like:
|