The switch statement is used to perform different actions based on different conditions. This article introduces the usage examples of switch statements in PHP, it involves the knowledge points of switch usage, and the friends who are interested in switch usage learn it together through this article. We hope to help you.
Switch is a switching statement, so many friends only know the simple switch switch statement usage, the following tutorial small series for you to introduce the switch usage example.
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, the continue 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"; >
Example #2 switch structure can be used in string
<?phpswitch ($i) {case "Apple": echo "I am Apple"; break, Case "bar": echo "I am bar"; case "cake": echo "I am C Ake "; break;}? >
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. Only when the value in one case statement matches the value of the switch expression does PHP begin executing the statement until the end of the switch's program segment (such as a return statement) or the first break statement is encountered. 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:
<?phpswitch ($i) {case 0:echo "I equals 0", Case 1:echo "I equals 1", Case 2:echo "I equals 2";}? >
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.
<?phpswitch ($i) {case 0:case 1:case 2:echo "I was less than 3 and not negative", break, Case 3:echo "I was 3";}? >
The exception to a case is default. It matches anything that doesn't match any other case. For example
<?phpswitch ($i) {case 0:echo "I equals 0", break, Case 1:echo "I equals 1", break, Case 2:echo "I equals 2"; Default:echo "I was not equal to 0, 1 or 2";}? >
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 = Strtolowe R ($val {strlen ($val)-1}); Switch ($last) {case ' G ': $val *=, Case ' m ': $val *=, case ' K ': $val *=,} 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 *= 1024; is executed, but because there is no break language, it will continue to hit case ' K ' and execute its next $val *= 1024; The statement, so, is generally equivalent to 400 * 1024 * 1024.
Related recommendations:
A detailed description of using PHP to find the longest common substring of two strings
A detailed explanation of how PHP is implemented to generate a VCF vcard file
PHP High-precision operation BC function library