The basic syntax structure of the PHP condition statement. In php, the conditional statements refer to ifelse, swicth, and third-order operators. next I will introduce some syntax for these conditional statements. If basic syntax structure of a single branch if (condition expression) {condition statements in php are the if else, swicth, and three-object operators, next I will introduce some syntax for these condition statements.
Basic syntax structure of if single branch
If (conditional expression ){
// Execute the statement;
}
If dual branch
Basic syntax structure
If (conditional expression ){
// Execute the statement;
} Else {
// Execute the statement;
}
If multiple branches
The basic syntax structure is
If (conditional expression ){
// Execute the statement;
} Else if (conditional expression ){
// Execute the statement;
} Else if (condition expression ){
// Execute the statement;
} Else {// more else if
// Execute the statement;
}
☞Note the following for the above structure:
1) else if can have one or more
2) else does not have
Example
| The code is as follows: |
|
|
If ($ account = "lord" & $ password = "pass "){
Echo "welcome $ _ POST [account]. your password is $ _ POST [password]";
} Elseif ($ _ POST [account] = "god" & $ _ POST [password] = "dog "){
$ Y = date ("Y")-1911;
$ M = date ("m ");
$ D = date ("d ");
Echo "welcome $ _ POST [account], today is the Republic of China". $ y. "year". $ m. "month". $ d. "day ";
} Else {
Echo "logon failed: entered information:
Account: $ _ POST [account]
Password: $ _ POST [password] ";
}
?> |
Switch branch statement
Basic syntax structure
Switch (expression ){
Case constant 1:
// Execute the statement;
Break;
Case constant 2:
// Execute the statement;
Break;
Defual:
// Execute the statement;
Break;
}
Example
The following two examples use different methods to achieve the same thing. The first one is to use the if... elseif... else statement, and the other one uses the switch statement.
| The code is as follows: |
|
Else {
Echo "I is not equal to 0, 1 or 2 ";
}
?>
|
The above PHP code indicates that if the variable $ I is equal to 0, "I equals 0" is output. if the variable $ I is equal to 1, "I equals 1" is output "; if the variable $ I is equal to 2, "I equals 2" is output; if none, "I is not equal to 0, 1 or 2" is output ".
Comparison between switch statements and elseif statements
In the switch statement, the condition is evaluated only once and then compared with each case. in the elseif statement, the condition is evaluated again. If your conditions are complex or repeated loops, the switch statement will be faster.
| The code is as follows: |
|
|
IsEmpty = "";
$ IsEmpty = "not empty ";
$ Test = empty ($ isEmpty )? "": Trim ($ isEmpty ); |
Three-object operator? : If the preceding condition empty ($ isEmpty) is true or false, is calculated? The following expression. if it is false, the value is calculated as follows.
If you are not used to it, you can change it to the following code:
| The code is as follows: |
|
|
If (empty ($ isEmpty) // The condition is true.
{
$ Cookie = "";
}
Else
{
$ Cookie = trim ($ isEmpty );
}
|
☞Note:
1) one or more case statements
2) defaul statements can be absent (based on the business logic of your code)
3) generally, after a case statement, a break is required to exit the switch statement.
4) constant type (int, float, string, Boolean)
Supplement
Comparison operator.
$ A = $ B is equal to TRUE if $ a is equal to $ B.
$! = $ B is not equal to TRUE, if $ a is not equal to $ B.
$ A <> $ B is not equal to TRUE, if $ a is not equal to $ B.
$ A <$ B small and TRUE, if $ a is strictly less than $ B.
$ A> $ B is greater than TRUE if $ a is strict with $ B.
$ A <= $ B is less than or equal to TRUE, if $ a is less than or equal to $ B.
$ A >=$ B is greater than or equal to TRUE if $ a is greater than or equal to $ B.
Logical operators.
$ A and $ B And (logical and) TRUE, if both $ a And $ B are TRUE.
$ A or $ B Or (logical or) TRUE, if $ a Or $ B is TRUE.
$ A xor $ B Xor (logical exclusive or) TRUE. if either $ a or $ B is TRUE, but not both.
! $ A Not (logical Not) TRUE, if $ a is Not TRUE.
$ A & $ B And (logical And) TRUE, if both $ a And $ B are TRUE.
$ A | $ B Or (logical Or) TRUE. if $ a Or $ B is set to TRUE.
Cancelse, swicth, and third-object operators. let's introduce some syntax of these conditional statements. If basic syntax structure of a single branch if (conditional expression ){...