PHP: Detailed explanation of the usage differences and operating efficiency of switch case and if else

伊谢尔伦
Release: 2023-03-11 09:58:02
Original
3194 people have browsed it

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 is similar 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, the other using a switch statement:

Example #1 switch structure

<?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;
}
?>
Copy after login

Example #2 The switch structure can use String

<?php
switch ($i)
{
    case "apple":
        echo "i is apple";
        break;
    case "bar":
        echo "i is bar";
        break;
    case "cake":
        echo "i is cake";
        break;
}
?>
Copy after login

Key point: (This is what I have never mastered before!)

To avoid errors, it is very 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 if the value in a case statement matches the value of the switch expression PHP does not start executing statements until the end of the switch program segment (such as the return statement) or until the first break statement is encountered. If break is not written at the end of the statement section of the case, PHP Execution of the statement segment in the next case will continue. For example:

<?php
switch ($i)
{
    case 0:
        echo "i equals 0";
    case 1:
        echo "i equals 1";
    case 2:
        echo "i equals 2";
}
?>
Copy after login

Special note: If $i equals 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] In the switch statement, the condition is only evaluated once and used to compare 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 only transfers control to the statements in the next case.

<?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";
}
?>
Copy after login

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

<?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";
}
?>
Copy after login

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 to simple types.

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

<?php
/**
 * 返回字节数
 *
 * @param string $val 如 400M
 */
function return_bytes($val = &#39;&#39;)
{
    $val = trim($val);
    $last = strtolower($val{strlen($val)-1});
    switch ($last)
    {
        case &#39;g&#39;:
            $val *= 1024;
        case &#39;m&#39;:
            $val *= 1024;
        case &#39;k&#39;:
            $val *= 1024;
    }
 
    return $val;
}
 
$memorylimit = ini_get(&#39;memory_limit&#39;);
echo $memorylimit, &#39;<br/>&#39;;
echo return_bytes($memorylimit);
Copy after login

Output:

400M
419430400
Copy after login

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

The above is the detailed content of PHP: Detailed explanation of the usage differences and operating efficiency of switch case and if else. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template