PHP Conditional Statements
When you write code, you often need to perform different actions for different judgments. You can use conditional statements in your code to accomplish this task.
In PHP, the following conditional statements are provided:
· Execute a block of code, and execute another block of code when the condition is not true
· if...else . Statement - Execute a block of code when one of several conditions is true
PHP - if statement
if statement is used Execute code only when specified conditions are true.
A simple IF statement consists of two parts. expr is our expression, and statement is the code we need to execute; when expr is true, the statement is executed; otherwise, it is ignored.if (expr)statement
expr must be placed between a pair of parentheses
Usually after the if condition is judged successfully, we Multiple statements need to be executed. In this case, you need to use {} to enclose the statements to form a code block
Syntax
if (condition)
{
## "Have a good day!":
Example
<?php $t=date("H"); if ($t<"20") { echo "Have a good day!"; } ?>
if (condition) { Code to be executed when the condition is true; }
else{
Code to be executed when the condition is not true Code;}
If the current time is less than 20, the following example will output "Have a good day!", otherwise it will output "Have a good night!":
Example
<?php $t=date("H"); if ($t<"20") { echo "Have a good day!"; } else { echo "Have a good night!"; } ?>
PHP - if...else if....else statement
To execute a block of code when one of several conditions is true, please use the if....else if...else statement.
Syntax
{ if code to be executed when the condition is true; } else if (condition) {
elseif Code to be executed when the condition is true;}
else{
Code to be executed when the condition is not true; }
If the current time is less than 10, the following example "Have a good morning!" will be output. If the current time is not less than 10 and less than 20, then "Have a good day!" will be output. Otherwise, "Have a good night!" will be output:
Example
<?php $t=date("H"); if ($t<"10") { echo "Have a good morning!"; } else if ($t<"20") { echo "Have a good day!"; } else { echo "Have a good night!"; } ?>
PHP - switch statement
The switch statement will be explained in the next chapter.
Ternary operator
PHP has a special operator , we did not introduce it when we learned operators before. After learning if and else
statements, we can introduce
(expr1) ? (expr2) : (expr3)
When the expr1 condition is true, the value is expr2, otherwise the value is expr3
Equivalent to conditional statement:
if (expr1) {
expr2
} else {
expr3
}
elseif statement
When multiple conditions appear, we can use elseif to construct a sequence of multiple options, which is equivalent to else+ A combination of if statements, so it is also possible to write else if
. Optimize the code
For example:
if ($dir == 'west') {
echo 'west';
} else if ($dir == 'east') {
echo 'east' ;
} else if ($dir == 'north') {
echo 'North';
} else if ($dir == 'sourth') {
echo 'South';
} else {
echo 'Unknown';
}
Note: When using else if, each code block is a mutually exclusive condition. In the end, only A block of code will be executed.