Home  >  Article  >  Backend Development  >  How to write php judgment statement

How to write php judgment statement

青灯夜游
青灯夜游Original
2021-07-15 14:14:056432browse

Writing method: 1. "if (condition) {statement block;} else if (condition) {statement block;} else {statement block;}"; 2. "switch (expression) {case value 1 :Statement block;break;...case value n:Statement block;break;default:Statement block;}".

How to write php judgment statement

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

In the process of program development, it is often necessary to In this case, different codes are executed. At this time, conditional judgment is needed. In PHP, if or switch can be used for conditional judgment. The corresponding conditional statement should be selected according to the actual scenario:

1. if else statement

The if else statement is a type of execution based on conditional judgment in process control. When this statement is executed, the condition is first judged, and then the corresponding operation is made based on the judgment result. It can be subdivided into three types, namely if statement, if...else statement, if...else if...else statement .

1), if conditional statement

if statement is the simplest type of process control. Only determine whether a certain condition is true, and if it is true, execute a specific statement block. The syntax format is as follows:

if (判断条件) {
    语句块;
}

If the statement block that needs to be executed contains more than one statement, you must use { } to enclose them to represent a whole; if there is only one statement to be executed, you can omit the curly brackets mark. Like the following:

if (判断条件)
    一条语句;

The execution flow of the if statement is shown in the figure below:

How to write php judgment statement

[Example] Define an array and use the if statement to determine whether the number is It is not an even number. The code is as follows:

<?php
    $num = 34;
    if ($num % 2 == 0) {
        echo &#39;$num =&#39;.$num.&#39;, 是偶数!&#39;;
    }
?>

The running result is as follows:

$num =34, 是偶数!

2), if...else...conditional statement

## The #if statement can only perform operations when the judgment result is true, which is not enough in many cases, so there are statements in the form of if...else. Different from the if judgment, the if...else statement not only performs operations on the situation where the judgment result is true, but can also perform corresponding operations on the situation where the judgment result is not true.

The else statement extends the if statement and can execute the corresponding statement when the value of the expression in the if statement is FALSE. Another thing to note is that the else statement is a clause of the if statement and must be used together with the if statement and cannot exist alone.

The syntax format of the if...else statement is as follows:

if (判断条件) {
    语句块 1;
} else {
    语句块 2;
}

In the above format, if the "judgment condition" is true, then execute "statement block 1"; otherwise, execute "statement Block 2". Both "Block 1" and "Block 2" can contain multiple statements. The same as the if statement, if both "Statement Block 1" and "Statement Block 2" contain only one statement, the curly brackets { } can be omitted, as follows:

if (判断条件)
    语句块 1;
else
    语句块 2;

if...else The execution flow of the statement is shown in the figure below:

How to write php judgment statement

Example] Use the rand() function to generate a random number $num, and then determine whether the random number is even or odd. The code is as follows As shown:

<?php
    $num = rand(1,31);  //生成一个 1~31 之间的随机数
    if ($num % 2 == 0) {
        echo &#39;$num =&#39;.$num.&#39;, 是偶数!&#39;;
    } else {
        echo &#39;$num =&#39;.$num.&#39;, 是奇数!&#39;;
    }
?>

The running results are as follows:

$num =27, 是奇数!

3), if...elseif...else...conditional statement

The else if statement is the same as the else statement. It extends the if statement. The else if statement determines which statement block to execute based on different expressions.

In PHP, you can also use the two else if keywords together (such as elseif). The syntax format of the else if statement is as follows:

if (判断条件 1) {
    语句块 1;
} else if (判断条件 2) {
    语句块 2;
} else if (判断条件 3) {
    语句块 3;
}
......
else if (判断条件 n) {
    语句块 n;
}
else{
    语句块 n+1;
}

In the above else if syntax, if the first "judgment condition 1" is TRUE, then the "statement block 1" statement is executed; if the second If "judgment condition 2" is TRUE, then the statement "statement block 2" will be executed; and so on. If none of the conditions of the expression is TRUE, the "statement block n 1" statement in the else clause is executed. Of course, the last else statement can also be omitted.

Only one expression in the else if statement can be TRUE at the same time, that is, only one statement block can be executed in the else if statement. If there are multiple expressions that evaluate to TRUE, only the statement block corresponding to the first expression will be executed.

if...else The execution flow of the if...else statement is shown in the figure below:

How to write php judgment statement

[Example] Judge the excellent or excellent results based on the results. Good, medium and poor levels, the code is as follows:

<?php
    $score = 89; 
    if ($score > 90) {
        echo &#39;成绩的级别为:优!&#39;;
    } else if ($score > 70) {
        echo &#39;成绩的级别为:良!&#39;;
    } else if ($score > 60) {
        echo &#39;成绩的级别为:中!&#39;;
    } else {
        echo &#39;成绩的级别为:差!&#39;;
    }
?>

The running results are as follows:

成绩的级别为:良!

2. switch...case...conditional statement

The switch statement is similar to the if...else if...else statement. It is also a branch structure. Compared with the if...else if...else statement, the switch statement is more concise. Clearly.

The switch statement consists of an expression and multiple case labels. The case label is followed by a code block, and the case label serves as the identifier of this code block. The syntax format of the switch statement is as follows:

switch(表达式){
    case 值 1:
        语句块 1;
        break;
    case 值 2:
        语句块 2;
        break;
    ... ...
    case 值 n:
        语句块 n;
        break;
    default:
        语句块 n+1;
}

switch 语句根据表达式的值,依次与 case 中的值进行比较,如果不相等,继续查找下一个 case;如果相等,就会执行对应的语句,直到 switch 语句结束或遇到 break 为止。

一般来说,switch 语句最终都有一个默认值 default,如果在前面的 case 中没有找到相符的条件,则执行默认语句,和 else 语句类似。

switch 语句的执行流程如下图所示:

How to write php judgment statement

在使用 switch 语句时应该注意以下几点:

  • 和 if 语句不同的是,switch 语句后面表达式的数据类型只能是整型或字符串,不能是 bool 型。通常这个表达式是一个变量名称,虽然 PHP 是弱类型语言,在 switch 后面表达式的变量可以是任意类型数据,但为了保证匹配执行的准确性,最好只使用整型或字符串中的一种类型。

  • 和 if 语句不同的是,switch 语句后面的花括号是必须有的。

  • case 语句的个数没有规定,可以无限增加。但 case 标签和 case 标签后面的值之间应该有一个空格,值后面必须有一个冒号,这是语法的一部分。

  • switch 匹配完成以后,将依次逐条执行匹配的分支模块中的语句,直到 switch 结构结束或者遇到了 break 语句才停止执行。所以,如果一个分支语句的后面没有写上 break 语句,程序将会继续执行下一个分支语句的内容。

  • 与 if 语句中的 else 类似,switch 语句中 default 标签直接在后面加上一个冒号,看似没有条件,其实是有条件的,条件就是“表达式”的值不能与前面任何一个 case 标签后的值相等,这时才执行 default 分支中的语句。default 标签和 if 中的 else 子句一样,它不是 switch 语句中必需的,可以省略。

【示例】使用 date() 函数获取当前星期的英文缩写,根据缩写打印今天是星期几,代码如下所示:

<?php
    $week = date(&#39;D&#39;);
    switch($week){
        case &#39;Mon&#39;:
            echo &#39;星期一&#39;;
            break;
        case &#39;Tue&#39;:
            echo &#39;星期二&#39;;
            break;
        case &#39;Wed&#39;:
            echo &#39;星期三&#39;;
            break;
        case &#39;Thu&#39;:
            echo &#39;星期四&#39;;
            break;
        case &#39;Fri&#39;:
            echo &#39;星期五&#39;;
            break;
        case &#39;Sat&#39;:
            echo &#39;星期六&#39;;
            break;
        case &#39;Sun&#39;:
            echo &#39;星期日&#39;;
            break;
    }
?>

运行结果如下:

星期四

推荐学习:《PHP视频教程

The above is the detailed content of How to write php judgment statement. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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