If, elseif, and else statements are used to execute different actions based on different conditions.
Condition Statement
When writing code, you often need to perform different actions for different judgments.
You can use conditional statements in the code to complete this task.
If... Else statement
Execute one piece of code when the conditions are met, and execute another piece of code when the conditions are not met.
Elseif statement
And if... Else is used together to execute a code block when several conditions are met.
If... Else statement
If you want to execute some code when a condition is set, and execute other code when the condition is not met, use if .... Else statement.
Syntax
If (condition)
Code to be executed if condition is true;
Else
Code to be executed if condition is false;
Instance
If the current date is Friday, the following code will output "Have a nice weekend !", Otherwise, "Have a nice day!" is output !" :
The code is as follows: |
Copy code |
<Html> <Body> <? Php $ D = date ("D "); If ($ d = "Fri ") Echo "Have a nice weekend! "; Else Echo "Have a nice day! "; ?> </Body> </Html> |
If you need to execute multiple lines of code when the conditions are true or fail, you should include these lines in curly brackets:
The code is as follows: |
Copy code |
<Html> <Body> <? Php $ D = date ("D "); If ($ d = "Fri ") { Echo "Hello! <Br/> "; Echo "Have a nice weekend! "; Echo "See you on Monday! "; } ?> </Body> </Html> |
ElseIf statement
To execute code when multiple conditions are created, use the elseif statement:
Syntax
If (condition)
Code to be executed if condition is true;
Elseif (condition)
Code to be executed if condition is true;
Else
Code to be executed if condition is false;
Instance
If the current date is Friday, the following example will output "Have a nice weekend !", If it is Sunday, output "Have a nice Sunday !", Otherwise, "Have a nice day!" is output !" :
The code is as follows: |
Copy code |
<Html> <Body> <? Php $ D = date ("D "); If ($ d = "Fri ") Echo "Have a nice weekend! "; Elseif ($ d = "Sun ") Echo "Have a nice Sunday! "; Else Echo "Have a nice day! "; ?> </Body> </Html> |