Conditional statement for performing different actions depending on the conditions.
PHP conditional statements
When you write code, you often need to perform different actions for different decisions. You can use conditional statements in your code to accomplish this task.
In PHP, we provide the following conditional statement:
if statement - execute code when conditions are met
if ... else statement - execute code when a condition is satisfied, another piece of code is executed when the condition is not satisfied
if ... elseif .... else statement - execute a block of code when one of several conditions are met
switch statement - execute a block of code when one of several conditions are met
PHP - if statement
if statement is used to execute code only when a specified condition is met.
grammar
if (condition)
{
When the conditions are met to execute the code;
}
If the current time is less than 20, the following examples will output "Have a good day!":
$t=date("H");
if ($t<"20")
{
echo "Have a good day!";
}
?>
PHP - if ... else statement
Execute a block of code when condition is met, another piece of code is executed when the condition is not satisfied, use the if .... else statement.
grammar
if (condition)
{
Code that executes when conditions are met;
}
else
{
Code that is executed when the condition is not satisfied;
}
If the current time is less than 20, the following examples will output "Have a good day!", And otherwise outputs "Have a good night!":
$t=date("H");
if ($t<"20")
{
echo "Have a good day!";
}
else
{
echo "Have a good night!";
}
?>
PHP - if ... elseif .... else Statement
Execute a block of code when one of several conditions are true, use the if .... elseif ... else statement. .
grammar
if (condition)
{
The code executed when if condition is true;
}
elseif (condition)
{
Code that executes when elseif conditions are met;
}
else
{
Code that is executed when the condition is not satisfied;
}
If the current time is less than 10, the following examples will output "Have a good morning!", If the current time is not less than 10 and less than 20, the output, or output "Have a good night!" "Have a good day!":
$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!";
}
?>
PHP - switch statement
The switch statement will explain in the next chapter.
View all PHP tutorial article: https://www.codercto.com/courses/l/5.html (edit: Leilin Peng Source: network intrusion deleted)