Detailed explanation of the 'elseif statement' example of PHP conditional control statement

怪我咯
Release: 2023-03-07 13:08:02
Original
5261 people have browsed it

We learned about the "if...else statement" before and learned that our "if...else statement" can only choose two results, either true or false. But what should we do when multiple conditions appear? What does it mean to have multiple items? To give an example, if there is a class exam, and after the exam results come out, the teacher will classify: those with a score of 90 or above are excellent; those with a score between 60-90 are good, and those with less than 60 are considered It's a failing grade. Under these multiple conditions, we need to use our "elseif statement".

Let’s first look at the syntax of “elseif statement”

if(条件表达式1){
            PHP语句1;
}elseif(条件表达式2){
            PHP语句2;
}...........
else{
          PHP语句 ;
}
Copy after login

Detailed explanation of the statement:

The meaning of the above statement is: when When our "conditional expression 1" is true, execute PHP statement 1. When it is not true, then execute "conditional expression 2". When conditional expression 2 is true, execute PHP statement 2, condition When expression 2 is not true, the execution continues..., and so on, until the conditional expression x is found, and when it is true, PHP statement x is executed. If, after execution to the end, there is no If the conditional expression is true, the PHP statement in else is executed.

Detailed explanation of the elseif statement example of PHP conditional control statementelseif can be written as else if

Detailed explanation of the elseif statement example of PHP conditional control statementEach PHP statement is mutually exclusive, and there will only be one PHP execution in the end

elseif Statement flow chart is as followsDetailed explanation of the elseif statement example of PHP conditional control statement

elseif statement example

This example uses the elseif statement to determine whether today is the first ten days of this month or the middle , or late, the code is as follows

<?php
header("Content-type:text/html;charset=utf-8");    //设置编码
$mouth=date("n");             //设置月份变量$mouth
$today=date("j");             //设置日期变量$today

if($today>=1 and $today<=10){                         //判断日期变量是否在1~10之间
    echo  "今天是".$mouth."月".$today."上旬";         //如果是,说明是上旬  
}else if($today>=10 and $today<=20){                   //判断日期变量是否在11~20之间
    echo "今天是".$mouth."月".$today."中旬";            //如果是,说明是中旬
}else{                                                  //如果上面的两个判断都不符合要求,则输出默认值
    echo "今天是".$mouth."月".$today."下旬";             //上面本月是下旬
}
?>
Copy after login

The output result is as shown below:

Detailed explanation of the elseif statement example of PHP conditional control statement

The above is our simple application of elseif.

Not only elseif can perform multiple selection judgments, but there is also a multiple judgment statement. In the next section, we will learn another multiple judgment statement "switch...case" statement.

The above is the detailed content of Detailed explanation of the 'elseif statement' example of PHP conditional control statement. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template