PHP has several judgment statements

青灯夜游
Release: 2023-03-16 11:16:01
Original
5912 people have browsed it

There are three types: 1. if else statement, syntax "if (condition) {code;}else if (condition) {code;}else{code;}"; 2. switch case statement, syntax " switch (expression) {case value 1: statement block; break;...case value n: statement block; break; default: statement block;}"; 3. "Expression 1? Expression 2: Expression 3" statement, one of the other two expressions will be selected and executed based on the result of expression 1.

PHP has several judgment statements

The operating environment of this tutorial: Windows 7 system, PHP version 8.1, DELL G3 computer

php has three judgment statements:

  • if else statement

  • switch case statement

  • ternary operation statement

1. if else statement

When the if statement is executed, it first judges the condition, and then makes a decision based on the judgment result. Corresponding operations. It can be subdivided into three types, namely if statement, if...else statement, if...else if...else statement.

  • if statement

The 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 (判断条件) {
    语句块;
}
Copy after login
  • if...else statement

if statement can only perform operations when the judgment result is true. In many cases it is not enough, 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;
}
Copy after login

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 braces { } can be omitted.

  • if...else if...else statement

else if statement is the same as else statement, it extends if statement, else The 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;
}
Copy after login

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.

Example:

 90) {
    echo '成绩的级别为:优!';
} else if ($score > 70) {
    echo '成绩的级别为:良!';
} else if ($score > 60) {
    echo '成绩的级别为:中!';
} else {
    echo '成绩的级别为:差!';
}
?>
Copy after login

PHP has several judgment statements

##2, switch case 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 and clear.

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;
}
Copy after login

The switch statement is compared with the value in the case in turn according to the value of the expression. If they are not equal, continue to search for the next case; if they are equal, the corresponding statement will be executed. , until the switch statement ends or break is encountered.

Generally speaking, the switch statement ultimately has a default value default. If no matching condition is found in the previous case, the default statement will be executed, similar to the else statement.

Example: Use the date() function to get the English abbreviation of the current week, and print the day of the week today based on the abbreviation

Copy after login

PHP has several judgment statements

##3 , Ternary operation statement Like the C language, there is also a ternary operator in PHP. The ternary operator can realize a simple conditional judgment function, that is, based on the first expression The result selects one of the other two expressions and executes it. The ternary operator is also called the ternary operator or conditional operator.

The function of the ternary operator is consistent with the "if else" statement. It can be written in one line, making the code concise and more efficient. Proper use of the ternary operator in PHP programs can make scripts more concise and efficient.

三元运算符的语法格式如下:

(expr1)?(expr2):(expr3); //表达式1?表达式2:表达式3
Copy after login

如果条件“expr1”成立,则执行语句“expr2”,否则执行“expr3”。

示例:

Copy after login

PHP has several judgment statements

推荐学习:《PHP视频教程

The above is the detailed content of PHP has several judgment statements. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!