if...else...elseif statement

PHP If...Else statement

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:

·                                                                                                                                                    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

The if statement is used to execute code only when a specified condition is true. Syntax

if (condition)

{

Code to be executed when the condition is true;

}

If the current time is less than 20, the following example will output "Have a good day!":

Example

<?php
 $t=date("H");
 if ($t<"20")
 {
     echo "Have a good day!";
 }
 ?>

PHP - if...else statement

To execute a block of code when a condition is true and another block of code when the condition is not true, please use the if....else statement. Syntax

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

Execute a block of code when one of several conditions is true, please use if... .else if...else statement. .Syntax

if (condition)

{

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 The 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!":

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!";
 }
 ?>

if statement

if(true){
         语句;
         语句;
}
if(bool) {
         多条
}
if(bool)
         单条

When the value of the condition is true (true), PHP will execute the statement group. On the contrary, when the value of the conditional expression is false (false), PHP will not execute the statement. Group, ignore the statement group and execute the subsequent statements.

PHP - if...else statement

if (条件)
 {
 条件成立时执行的代码;
 }
 else
 {
 条件不成立时执行的代码;
 }

if-else conditional judgment is similar to if conditional judgment, but the difference is that when the conditional expression of if -else statement When the value of the formula is true (true), the body statement of if (statement group 1) will be executed, and when the value of the conditional expression is false (false), the body statement of else (statement group 2) will be executed

PHP - elseif statement

if(条件){
}else  if (条件2){
}else if (条件3){
}..........
 
if(条件){
}elseif(条件2){
}elseif (条件3){
}..........
else{
};

Code example

<?php
     $score = 67;
     if ($score >=90 && $score <=100){
         echo "优秀";
     }else if($score >=80 && $score <90) {
         echo "良";
     } elseif($score >=70 && $score <80){
         echo "好";
     }else if($score >=60 && $score <70){
         echo "一般";
     }elseif($score >=0 && $score<60){
         echo "差";
     }else{
         echo "分数不符合范围";
     };
 
 ?>

Note: In this multi-way branch, only one


can be entered
Continuing Learning
||
<?php $t=date("H"); if ($t<"20") { echo "Have a good day!"; } else { echo "Have a good night!"; } ?>
submitReset Code