Home >Backend Development >PHP Problem >What is PHP Switch statement? Detailed explanation of switch statement usage
The previous article introduced you to "How do you understand the use of the string function library? Let’s discuss it together! ! ! ", this article continues to introduce to you what the PHP Switch statement is and how to use the Switch statement. If you are a "newbie", come and learn together! ! !
Usage of PHP Switch statement: The switch statement is used to perform different actions based on multiple different conditions.
The switch statement flow chart is as follows:
The syntax is as follows:
switch (n) { case 1: 如果 n=1,此处代码将执行; break; case 2: 如果 n=2,此处代码将执行; break; default: 如果 n 既不等于 1 也不等于 2,此处代码将执行; }
Working principle: First, A simple expression n (usually a variable) is evaluated once. Compares the value of the expression to the value of each case in the structure. If there is a match, the code associated with the case is executed. After the code is executed, use break to prevent the code from jumping to the next case to continue execution. The default statement is executed when there is no match (that is, no case is true).
The following is a specific code demonstration:
<?php $fiml="1"; switch ($fiml) { case "1": echo "你喜欢的电影是《我的姐姐》!"; break; case "2": echo "你喜欢的电影是《如果声音不记得》!"; break; case "3": echo "你喜欢的电影是《误杀》!"; break; default: echo "你没有喜欢的电影!"; } ?>
The running results are as follows:
Recommended learning: "PHP Video Tutorial》
The above is the detailed content of What is PHP Switch statement? Detailed explanation of switch statement usage. For more information, please follow other related articles on the PHP Chinese website!