The "advanced" usage of the PHP switch judgment statement is detailed in the switch
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
Copy the Code code 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 switch structure can be used in string
Copy the Code code 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
}
?>
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:
Copy the Code code as follows:
<?php
Switch ($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.
Copy the Code code as follows:
<?php
Switch ($i)
{
Case 0:
Case 1:
Case 2:
echo "I was less than 3 but not negative";
Break
Case 3:
echo "I is 3";
}
?>
The exception to a case is default. It matches anything that doesn't match any other case. For example:
Copy the Code code 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 am 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
Copy the Code code as follows:
<?php
/**
* Number of bytes returned
*
* @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, '
';
echo return_bytes ($memorylimit);
Output:
Copy the Code code as follows:
400M
419430400
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.
How does PHP submit the value after it is judged? I use the switch loop statement to judge, how to put the new value after the judgment is submitted to the new page
Save the session and the other page will be removed.
Switch ($a) {
Case "1":
echo "A";
if (!isset ($_session)) {
Session_Start ();
}
$_session[' A ']= ' a ';
Break
Another page:
if (!isset ($_session)) {
Session_Start ();
}
$a =$_session[' a '];
PHP switch statements are judged by the 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)
http://www.bkjia.com/PHPjc/887750.html www.bkjia.com true http://www.bkjia.com/PHPjc/887750.html techarticle The "advanced" usage of the PHP switch judgment statement is explained in detail, and the switch details only so called "advanced" usage, because I have not mastered the most basic usage of switch, so, next ...