In php, the conditional control language includes the if statement, swicth statement, and three non-operators. Below we will summarize some of the three Application Instances in php, I hope this tutorial will be helpful to all of you. In php, the conditional control language includes the if statement, swicth statement, and three non-operators. Below we will summarize some of the three Application Instances in php, I hope this tutorial will be helpful to all of you.
Script ec (2); script
There are three PHP conditional control statements:
First: if statement;
Type 2: switch statement;
Third: ternary operators;
The three condition control statements are described respectively.
First: if statement.
The if syntax has the following formats:
1 ),
If (expression 1) {// The following program segment is executed to meet the conditions
Execute the statement;
}
For example:
The Code is as follows: |
|
If ($ a> $ B ){ Echo "a is bigger than B "; $ B = $; } ?> |
2 ),
If (expression 1) {// The following program segment is executed to meet the conditions
Execute the statement;
}
Else {// does not meet the following procedure section
Execute the statement;
}
For example:
The Code is as follows: |
|
If ($ a> $ B ){ Echo "a is bigger than B "; } Else { Echo "a is NOT bigger than B "; } ?> |
3), if (expression 1) {// The following program segment is executed to meet the conditions
...
} Elseif (expression 2 ){
...
} Else {
...
}
For example:
The Code is as follows: |
|
$ A = 45; $ B = 50; If ($ a> $ B) {print "a is greater than B ";} Elseif ($ a = $ B) {print "a is equal to B ";} Else {print "a less than B ";} ?> |
Type 2: switch statement.
A Switch is a multi-path branch statement. Although multiple nested if statements can be used to implement multiple branches, the deeper the nesting, the worse the program readability. Use the Switch statement to avoid lengthy code blocks such as if... elseif... else.
Switch statement syntax
Switch (var ){
Case value 1:
Statement 1;
Break;
Case value 2:
Statement 2;
Break;
Case value N:
Statement N;
Break;
Default:
Statement;
}
For example:
The Code is as follows: |
|
$ I = 1; 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 no equals "; Break; } ?> |
Third: ternary operators.
PHP script language support operator? :, Which is a compact version of the if statement. We can use the following example to explain the ternary OPERATOR:
Ternary operator Syntax:
(Condition )? 'Execution statement 1': 'execution statement 2 ′;
Explanation: if the condition is TRUE, Statement 1 is executed. If the condition is FALSE, Statement 2 is executed;
For example:
The Code is as follows: |
|
Echo ($ a> $ B )? "A is greater than B": "a is not greater than B "; |
Explanation:
If $ a> $ B, enter a greater than B. If $ a <$ B, enter a not greater than B.