If... Else of the PHP control statement. If, elseif, and else statements are used to execute different actions based on different conditions. Conditional statements when writing code, you often need to perform different actions for different judgments. You can use the If, elseif, and else statements 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
Use with if... else 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 set, use the 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 it will output" Have a nice day! ":
<?php$d=date("D");if ($d=="Fri") { echo "Hello! "; echo "Have a nice weekend!"; echo "See you on Monday!"; }?> |
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 output" Have a nice day! ":
<?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!"; ?> |
Explain and else statements are used to execute different actions based on different conditions. Conditional statements when writing code, you often need to perform different actions for different judgments. You can...