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.
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 (判断条件) { 语句块; }
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; }
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; }
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:
<?php header('content-type:text/html;charset=utf-8'); $score = 89; if ($score > 90) { echo '成绩的级别为:优!'; } else if ($score > 70) { echo '成绩的级别为:良!'; } else if ($score > 60) { echo '成绩的级别为:中!'; } else { echo '成绩的级别为:差!'; } ?>
##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; }
<?php header('content-type:text/html;charset=utf-8'); $week = date('D'); switch($week){ case 'Mon': echo '星期一'; break; case 'Tue': echo '星期二'; break; case 'Wed': echo '星期三'; break; case 'Thu': echo '星期四'; break; case 'Fri': echo '星期五'; break; case 'Sat': echo '星期六'; break; case 'Sun': echo '星期日'; break; } ?>
##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
如果条件“expr1”成立,则执行语句“expr2”,否则执行“expr3”。
示例:
<?php header('content-type:text/html;charset=utf-8'); $a = 10; $a % 2 == 0 ? print '$a 是偶数!' : print '$a 是奇数!'; ?>
推荐学习:《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!