PHP If...Else
Conditional statements are used to perform different actions based on different conditions.
PHP Conditional Statement
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:
if conditional statement - Execute code when the condition is true
if...else statement - execute a block of code when the condition is true, and execute another block of code when the condition is not true
if ...else if....else statement - Execute a block of code when one of several conditions is true
switch statement - Execute a block of code when one of several conditions is true Execute a code block when one of them is true
PHP - if conditional statement
if conditional statement is used to execute code only when the specified condition is true .
Syntax
{
Code to be executed when the condition is true;
}
If the current When the time is less than 20, the following example will output "Have a good day!":
Instance
<?php $t=date("H"); if ($t<"20") { echo "Have a good day!"; } ?>
Run Example»
Click the "Run Example" button to view the online example
PHP - if...else statement
Execute a block of code when the condition is true, and when the condition is not true When executing another block of code, please use if....else statement.
Syntax
{
Code executed when the condition is true;
}
else
{
Code executed when the condition is not true;
}
If the current time is less than 20, the following example will output "Have a good day!", otherwise Output "Have a good night!":
Instance
<?php $t=date("H"); if ($t<"20") { echo "Have a good day!"; } else { echo "Have a good night!"; } ?>
Run Instance»
Click the "Run Instance" button View online examples
PHP - if...else if....else statement
Execute a code block when one of several conditions is true, please use if....else if...else statement. .
Syntax
{
if Code executed when the condition is true;
}
else if (Condition)
{
elseif Code executed when the condition is true;
}
else
{
Code executed when the condition is not true;
}
If the current time is less than 10, the following example will output "Have a good morning!", if the current time is not less than 10 and less than 20, then output "Have a good day!", otherwise output "Have a good night!" ":
Instance
<?php $t=date("H"); if ($t<"10") { echo "Have a good morning!"; } elseif ($t<"20") { echo "Have a good day!"; } else { echo "Have a good night!"; } ?>
Run Instance»
Click the "Run Instance" button to view the online instance
Recommended practical tutorials: "PHP if...else...elseif statement"
PHP - switch statement
switch statement will be explained in the next chapter.