This article mainly introduces the usage of switch statement in PHP. Interested friends can refer to it. I hope it will be helpful to everyone.
The 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, the other using a switch statement:
Example #1 switch structure
Example #2 The switch structure can use strings
Key points: (This is what I have not mastered before!)
To avoid errors, it is important to understand how switch is implemented. 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:
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 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.
A special case of case is default. It matches any case that does not match any other case. For example:
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.
[Practical] Based on the above knowledge points, write a function like this: Calculate the number of bytes actually represented by the capacity value
'; echo return_bytes($memorylimit); 输出: 400M 419430400
Special note: When $val = 400M, case 'm' is Hit, the $val *= 1024; below it is executed, but because there is no break language, case 'k' will continue to be hit, and the $val *= 1024; statement below it will be executed, so, overall it is equivalent to execution 400*1024*1024.
Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.
Related recommendations:
PHP common file operation functions and example analysis
php method to obtain file suffix name
PHP method to get the year, month, day, hour, minute, and second between specified time periods
The above is the detailed content of Usage of switch statement in PHP. For more information, please follow other related articles on the PHP Chinese website!