This is done by providing a numeric argument tocontinueto tell it how many levels of enclosing loops should be skipped – and it counts the switch statement as a loop for this purpose. For example: $array = [1, 2, 3]; foreach($array as $vartem) { switch($vartem) { case 1: ech...
In this tutorial you will learn how to use the switch-case statement to test or evaluate an expression with different values in PHP.PHP If…Else Vs Switch…CaseThe switch-case statement is an alternative to the if-elseif-else statement, which does almost the same thing. The switch-case ...
Warning:If you omit thebreakstatement in a case that is not the last, and that case gets a match, the next case will also be executed even if the evaluation does not match the case! Example What happens if we remove thebreakstatement from case "red"?
4 PHP switch-case Statement 5 6 7 8 <?php 9 $today = date("D"); 10 switch($today){ 11 case "Mon": 12 echo "Today is Monday. Clean your house."; 13 break; 14 case "Tue": 15 echo "Today is Tuesday. Buy some food."; 16 break; 17 case "Wed": 18 echo "Today...
. Thebreakstatement would abort the conditional check if the case matched. Example Code: #php 7.x<?php$favfood="spaghetti";switch($favfood){case"momo":echo"Your favorite food is momo!";break;case"pizza":echo"Your favorite food is pizza!";break;case"burger":echo"Your favorite food is...
PHP'sswitchdoesn't just allow you to switch on the value of a particular variable: you can use any expression as one of the cases, as long as it gives a value for thecaseto use. As an example, here's a simple validator written usingswitch: ...
<?php switch ($i) { case0: echo"i equals 0"; break; case1: echo"i equals 1"; break; case2: echo"i equals 2"; break; default: echo"i is not equal to 0, 1 or 2"; } ?> 注意:如果有多个 default 将导致E_COMPILE_ERROR错误。
PHP switch Statement ExampleLet's consider a simple example where we want to determine the day of the week based on a numeric input.<?php $dayNumber = 3; switch ($dayNumber) { case 1: echo "Monday"; break; case 2: echo "Tuesday"; break; case 3: echo "Wednesday"; break; // ...
switch (test_expression) { case expected_value_1: statement_11; statement_12; ... [break;] case expected_value_2: statement_21; statement_22; ... [break;] ... ... default: statement_x1; statement_x2; ... [break;] } where "test_expression" is used to generate a test value ...
The PHP switch alternative syntax There is no switch shorthand like the if statement for example but there is instead an alternative for the above syntax. php<?php $role = "editor"; switch ($role): case "admin": echo "I am the one that knows your browser history"; break; case "edito...