Home  >  Article  >  Backend Development  >  Detailed explanation of the "advanced" usage of PHP's switch judgment statement, detailed explanation of switch_PHP tutorial

Detailed explanation of the "advanced" usage of PHP's switch judgment statement, detailed explanation of switch_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:17:491326browse

Detailed explanation of the "advanced" usage of PHP's switch judgment statement, detailed explanation of switch

The reason why it is called "advanced" usage is because I haven't even mastered the most basic usage of switch, so what I will talk about next is actually its basic usage!

A switch statement is similar to a series of IF statements with the same expression. There are many situations where you need to compare the same variable (or expression) with many different values ​​and execute different code depending on which value it equals. This is exactly what the switch statement is for.

Note: Note that unlike other languages, the continue statement acts similarly to break when applied to switch. If you have a switch in a loop and want to continue to the next iteration in the outer loop, use continue 2.

The following two examples use two different methods to achieve the same thing, one using a series of if statements and the other using a switch statement:

Example #1 switch structure

Copy code The code is as follows:

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 use strings

Copy code The code is as follows:

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 important to understand how switch is executed. The switch statements are executed line by line (actually statement by statement). Initially no code is executed. Only when the value in a case statement matches the value of the switch expression will PHP start executing the statement until the end of the switch block (such as a return statement) or until the first break statement is encountered. If you do not write break at the end of the statement segment of the case, PHP will continue to execute the statement segment in the next case. For example:

Copy code The code is as follows:

switch ($i)
{
case 0:
echo "i equals 0";
case 1:
echo "i equals 1";
case 2:
echo "i equals 2";
}
?>

Special note: If $i is equal to 3, PHP will not execute any echo statement! However, if $i equals 0, PHP will execute all echo statements! If $i equals 1, PHP will execute the next two echo statements. Only if $i equals 2 do you get the "expected" result - just "i equals 2". Therefore, it is important not to forget break statements (even when you deliberately want to avoid providing them in some cases).

[Efficiency] The condition in the switch statement is only evaluated once and compared with each case statement. The condition is evaluated again in the elseif statement. If the condition is more complex than a simple comparison or is in a loop many times, it may be faster to use a switch statement.

The statements in a case can also be empty, which just transfers control to the statements in the next case.

Copy code The code is as follows:

switch ($i)
{
case 0:
case 1:
case 2:
echo "i is less than 3 but not negative";
break;
case 3:
echo "i is 3";
}
?>

A special case of case is default. It matches any case that does not match any other case. For example:

Copy code The code is as follows:

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 expression that evaluates to a simple type, i.e. an integer or floating point number and a string. Arrays or objects cannot be used unless they are dereferenced to simple types.


[Practical Combat] Based on the above knowledge points, write a function like this: Calculate the number of bytes actually represented by the capacity value

Copy code The code is as follows:

/**
* Return 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, '
';
echo return_bytes($memorylimit);

Output:

Copy code The code is as follows:

400M
419430400

Special note: When $val = 400M, case 'm' is hit, and the following $val *= 1024; is executed, but because there is no break language, case 'k' will continue to be hit, and the following will be executed. The $val *= 1024; statement, so, is overall equivalent to executing 400 * 1024 * 1024.

How to submit the judged value in php? I use a switch loop statement to make the judgment. How can I submit the new value after judgment to a new page?

Save the session and take out the session on another page.
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'];

The php switch statement is judged based on 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)

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/887750.htmlTechArticleDetailed explanation of the "advanced" usage of PHP's switch judgment statement. The detailed explanation of switch is only called "advanced" usage. It is Because I haven’t even mastered the most basic usage of switch, so, let’s talk about it next...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn