Multi-branch in php refers to the PHP multi-directional conditional branch structure; in PHP, the elseif clause is a typical multi-directional conditional branch. It extends the if statement. The elseif clause will be based on different expression values. Determine which statement block to execute; in PHP, elseif can also be separated into two keywords else if for use.
#The operating environment of this article: Windows7 system, PHP7.1, Dell G3.
php What does multi-branching mean?
PHP branch control statement, branch structure of PHP process control structure
Flow control is universal and universal for any programming language, and is an important part of the program component. It can be said that in any programming language, three basic structures need to be supported: sequential structure, branch structure (selection structure or conditional structure) and loop structure. For sequential structures, mainly assignment statements and input/output statements, etc., that is, they are executed in order. There is nothing to say here. Here, the author focuses on summarizing the branch structure and loop structure.
Branch structure
The branch structure changes the order of program execution according to the required conditions during the execution of the program. That is, when the condition is met, a certain description block is executed, and otherwise, another description block is executed. The use of branch structures in programs can have the following forms:
单一条件分支结构 双向条件分支结构 多向条件分支结构 巢状条件分支结构
Single conditional branch
The if structure is a single conditional branch structure, and the basic format of the if statement is An expression is evaluated, and based on the calculation result, it is decided whether to execute the following statements. The "expression" in parentheses after if is the condition for execution, and the result returned by the condition can only be a Boolean value. It is usually the result value calculated by an expression composed of comparison operators or logical operators, or some functions that return Boolean type, etc. If a value of other types is passed in, it will be automatically converted to Boolean TRUE or FALSE. If the expression is TRUE, the code block is executed, otherwise it is not executed.
DEMO
<?php $x = 10; //定义一个整型变量$x,值为10 $y = 20; //定义一个整型变量$y,值为20 if ( $x < $y ) { //$x是小于$y的,所以执行下面语句块 $t = $x ; //先将$x的值放到临时的变量$t中 $x = $y ; //再将变量$y的值赋给变量$a $y = $t ; //再将临时变量$t中的值赋给变量$y } //语句块结束的花括号 var_dump($x > $y ); //两个变量的值已经交换,输出true echo $x." ".$y; ?>
Bidirectional conditional branch
Bidirectional conditional branch is like an if statement that can also contain an else clause, which means that it needs to satisfy a certain One statement is executed when a condition is met, and other statements are executed when the condition is not met. This is the function of the else clause. else extends the if statement and can execute the statement when the expression in the if statement evaluates to FALSE. It is worth noting that the else statement is a clause of the if statement and must be used together with if and cannot exist alone.
DEMO
<?php $x = 30; $y = 20; if( $x > $y ) { echo "变量$x 大于变量 $y <br>"; //判断的条件成立,此句会执行 } else { echo "变量$x 小于变量 $y <br>"; //判断的条件不成立,此句会被执行 } echo "变量$x和变量$y比较完毕 "; ?>
Multi-directional conditional branch
The elseif clause is a typical multi-directional conditional branch. It extends the if statement. The elseif clause will Determine which statement block to execute based on different expression values. In PHP, elseif can also be separated into two keywords else if for use. The execution sequence is that if expression l is TRUE, then execute the code block 1 statement; if it is judged that expression 2 is TRUE, then execute the code block 2 statement; and so on, if the nth expression is judged to be TRUE, then the code block is executed. n statement; if the conditions of the expression are not TRUE, the code block n l statement in the else clause is executed. Of course, the last else statement can also be omitted.
DEMO
<?php $week = date("D"); //获取当前的星期值,如Mon、Tue、Wed等 if ( $week == "Mon" ) { echo "星期一"; } elseif ( $week == "Tue" ) { echo "星期二"; } elseif ( $week == "Wed" ) { echo "星期三"; } elseif ( $week == "Thu" ) { echo "星期四"; } elseif ( $week == "Fri" ) { echo "星期五"; } elseif ( $week == "Sat" ) { echo "星期六"; } elseif ( $week == "Sun" ) { echo "星期日"; } ?>
The switch statement is similar to elseif and is also a multi-directional conditional branch structure, but if and elseif statements use Boolean expressions or Boolean values as branch conditions for branch control; and The switch statement is used to test the value of an expression and select and execute the corresponding branch program based on the test results to achieve branch control. The switch statement consists of a selection expression and multiple case labels. The case labels are followed by a code block. When using the switch statement, you should pay attention to the following points:
The data type of the selection expression after the switch statement can only be integer or string, not boolean. Usually this control expression is a variable name.
The curly braces after the switch statement are required.
The number of case statements is not specified and can be increased indefinitely. But there should be a space between the case tag and the value that follows it, and there must be a colon after the value, which is part of the syntax.
After the switch matching is completed, the statements in the matched branch modules will be executed one by one, and execution will not stop until the switch structure ends or a break statement is encountered.
The default label in the switch statement is directly followed by a colon, which means that the value of the expression cannot be equal to the value after any previous case label, and then default is executed. statement in the branch. The default tag can be omitted.
DEMO
<?php $week = date("D"); switch( $week ) { case "Mon": echo "星期一"; break; case "Tue": echo "星期二"; break; case "Wed": echo "星期三"; break; case "Thu": echo "星期四"; break; case "Fri": echo "星期五"; break; case "Sat": echo "星期六"; break; case "Sun": echo "星期日"; break; } ?>
Nested conditional branch
The nested conditional branch structure is the nesting of if statements, which means that the code block after if or else contains if statement.
DEMO
<?php $sex = "male"; //用户输入的性别 $age = 25; //用户输入的年龄 if ( $sex == "male" ) { //如果用户输入的是男性则执行下面的区块 if ( $age >= 60 ) { //如果是男性并且年龄在60以上则执行下面的区块 echo "这个先生已退休".($age-60)."年了"; } else { //如果是男性并且年龄在60以下则执行下面的区块 echo "这个先生在工作,还有".(60-$age)."年才能退休"; } } else { //如果用户输入的是女性则执行下面的区块 if( $age >= 55 ) { //如果是女性并且年龄在55以上则执行下面区块 echo "这个女士已退休".($age-55)."年了"; } else { //如果是女性并且年龄在55以下则执行下面区块 echo "这个女士在工作,还有".(55-$age)."年才能退休"; } } ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What does php multi-branch mean?. For more information, please follow other related articles on the PHP Chinese website!