Only so called " advanced " usage, because I even switch the most basic usage has not mastered, so, the next is actually the basic usage of it!
A switch statement is similar to a series of IF statements that have the same expression. In many cases it is necessary to compare the same variable (or expression) with many different values and execute different code depending on which value it equals. This is the purpose of the switch statement.
Note: Note that unlike other languages, thecontinue statement acts on a switch as if it were a break. If there is a switch in the loop and want to continue to the next reincarnation in the outer loop, use continue 2.
The following two examples use two different methods to accomplish the same thing, one with a series of if statements, and the other with a switch statement:
Example #1 Switch Structure
<?PHPIF ($i = = 0) {echo "I equals 0";} ElseIf ($i = = 1) {echo "I equals 1";} ElseIf ($i = = 2) {echo "I equals 2";} Switch ($i) {case 0:echo "I equals 0", break;case 1:echo "I equals 1"; Break;case 2:echo "I equals 2"; break;}? >
Example #2 switch structure can be used in string
<?phpswitch ($i) {case "Apple": echo "I am Apple"; Break;case "bar": echo "I am Bar"; Break;case "cake": echo "I is cake"; bre AK;}? >
Focus: (This is the place I have not mastered before!) )
To avoid errors, it is important to understand how the switch is executed. The switch statement executes one line after the other (in fact, a statement). No code was executed at the beginning. PHP only starts executing statements when the value in one case statement matches the value of the switch expression, until the end of the Switch 's program segment (such as a return statement) or when the first break statement. If you do not write a break at the end of the statement segment of the case, PHP continues to execute the statement segment in the next case. For example:
Special Note : here if $i equals 3,php will not execute any ECHO statement! However, if $i equals 0,php will execute all the echo statements! If $i equals 1,php, the following two echo statements are executed. Only if the $i equals 2 o'clock will the result be "expected"-only "I equals 2" is displayed. So, don't forget that break statements are important (even if you deliberately want to avoid providing them in some cases).
[efficiency] the condition in the switch statement is only one time and is used to compare with each case statement. The condition is evaluated again in the ElseIf statement. If the condition is much more complex than a simple comparison or in a multiple loop, then the switch statement may be faster.
The statement in one case can also be empty, so that the control is simply transferred to the statement in the next scenario.
The exception to a case is default. It matches anything that doesn't match any other case. For example:
The case expression can be any expression that evaluates to a simple type, that is, an integer or floating-point number, and a string. Arrays or objects cannot be used unless they are dereferenced as simple types.
" actual combat " according to the above knowledge point, write such a function: Calculate the capacity value actually represents the number of bytes
<?php/** * Returns the number of bytes * * @param string $val such as 400M */function return_bytes ($val = ") {$val = Trim ($val); $last = Strtolower ( $val {strlen ($val)-1}), switch ($last) {case ' G ': $val *= 1024;case ' m ': $val *= 1024;case ' k ': $val *= 1024;} return $val;} $memorylimit = Ini_get (' memory_limit '); Echo $memorylimit, ' <br/> '; Echo return_bytes ($memorylimit);
Output:
400m419430400
Special Note:$val = 400M, Case' m ' is hit, the $val below it is *= , it is executed, but because there is no break language, it will continue to hit cases ' K ', and performed under its $val *= 1024x768; Statement, so, is generally equivalent to the implementation of the * * 1024x768 * .
Reference:
http://php.net/manual/en/control-structures.switch.php
A detailed description of the "advanced" usage of PHP switch