PHP switch judgment statement "advanced" Usage Details, switch details
It is called "advanced" usage only because I have not mastered the basic usage of the switch. so, I will talk about the basic usage of the switch!
The switch statement is similar to a series of IF statements with the same expression. In many cases, you need to compare the same variable (or expression) with many different values and execute different codes based on the value it equals. This is exactly the purpose of the switch statement.
Note: unlike other languages, the function of the continue Statement on the switch is similar to break. If there is a switch in the loop and you want to continue to the next cycle in the outer loop, use continue 2.
The following two examples use two different methods to implement the same thing: one uses a series of if statements and the other uses the switch statement:
Example #1 switch structure
Copy codeThe Code is as follows:
<? Php
If ($ 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 the switch structure can be a string
Copy codeThe Code is as follows:
<? Php
Switch ($ I)
{
Case "apple ":
Echo "I is apple ";
Break;
Case "bar ":
Echo "I is bar ";
Break;
Case "cake ":
Echo "I is cake ";
Break;
}
?>
Key points: (this is what I have never mastered before !)
To avoid errors, it is very important to understand how the switch is executed. The switch statement is executed one by one row (in fact, it is a statement ). No code is executed at the beginning. PHP executes the statement only when the value in a case statement matches the value of the switch expression until the switch program segment ends (such as the return Statement) or the first break statement is encountered. If break is not written at the end of the case statement segment, PHP will continue to execute the statement segment in the next case. For example:
Copy codeThe Code is as follows:
<? Php
Switch ($ I)
{
Case 0:
Echo "I equals 0 ";
Case 1:
Echo "I equals 1 ";
Case 2:
Echo "I equals 2 ";
}
?>
NOTE: If $ I is equal to 3, PHP will not execute any echo statement! However, if $ I is equal to 0, PHP will execute all the echo statements! If $ I is equal to 1, PHP will execute the following two echo statements. Only when $ I is equal to 2 will the expected result be returned -- only "I equals 2" will be displayed ". So don't forget that the break statement is very important (even if you deliberately want to avoid providing them in some cases ).
[Efficiency] In the switch statement, the condition is evaluated only once and used to compare with each case statement. In the elseif statement, the condition is evaluated again. If the condition is much more complex than a simple one, or in a loop that occurs many times, the use of the switch statement may be faster.
The statement in a case can also be empty, which transfers the control to the statement in the next case.
Copy codeThe Code is as follows:
<? Php
Switch ($ I)
{
Case 0:
Case 1:
Case 2:
Echo "I is less than 3 but not negative ";
Break;
Case 3:
Echo "I is 3 ";
}
?>
The special case of a case is default. It matches any situations that do not match other cases. For example:
Copy codeThe Code is as follows:
<? Php
Switch ($ I)
{
Case 0:
Echo "I equals 0 ";
Break;
Case 1:
Echo "I equals 1 ";
Break;
Case 2:
Echo "I equals 2 ";
Break;
Default:
Echo "I is not equal to 0, 1 or 2 ";
}
?>
A case expression can be any simple expression, that is, an integer, a floating point, or a string. Arrays or objects cannot be used unless they are unbound from the simple type.
[Practice] based on the knowledge above, write a function: calculate the actual number of bytes for the capacity value.
Copy codeThe Code is as follows:
<? Php
/**
* Number of returned bytes
*
* @ Param string $ val for example, 400 M
*/
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:
Copy codeThe Code is as follows:
400 M
419430400
Note: When $ val = 1024 m, case 'M' is hit and $ val * =; is executed, but because there is no break language, therefore, it will continue to hit case 'K' and execute the $ val * = 1024; statement, so, which is equivalent to executing 400*1024*1024.
How does php submit the final value? I used the switch loop statement to determine how to submit the new value to the new page.
Save it to the session and retrieve the session from the other page.
Switch ($ ){
Case "1 ":
Echo "";
If (! Isset ($ _ SESSION )){
Session_start ();
}
$ _ SESSION ['a'] = 'a ';
Break;
Another page:
If (! Isset ($ _ SESSION )){
Session_start ();
}
$ A = $ _ SESSION ['a'];
Php switch statement determined by number of days
Var iMonth = 5;
Var quarter = ""
Switch (iMonth ){
Case 1 :;
Case 2 :;
Case 3 :;
Quarter = "chunji ";
Break;
Case 4 :;
Case 5 :;
Case 6 :;
Quarter = "xiaji ";
Break;
Case 7 :;
Case 8 :;
Case 9 :;
Quarter = "qiuji ";
Break;
Case 10 :;
Case 11 :;
Case 12 :;
Quarter = "dongji ";
Break;
}
Document. write (quarter)