Home > Backend Development > PHP Problem > What does the select structure statement in php mean?

What does the select structure statement in php mean?

WBOY
Release: 2023-03-15 15:44:02
Original
2093 people have browsed it

In PHP, selecting a structural statement means making judgments on some conditions to determine the statement to execute the specified code. The select result statement is used to judge a given condition and control the flow of the program based on the result of the judgment.

What does the select structure statement in php mean?

The operating environment of this article: Windows 10 system, PHP version 7.1, Dell G3 computer.

What does the selection structure statement in php mean?

The selection structure statement refers to the need to judge some conditions to decide to execute the specified code.

The selection structure is used to judge given conditions and control the flow of the program based on the results of the judgment.

Let’s take a look at the commonly used selection control statements:

1. if single-branch structure

Single-branch statement conditional judgment statement is also called a single-branch statement. When a certain condition is met, certain processing is performed. For example, only if the age is greater than or equal to 18 years old, adult will be output, otherwise there will be no output. Specific syntax and examples are:

Grammar

if(判断条件){
         代码段
}
Copy after login

Example

if($age>=18){
         echo’已成年’;
}
Copy after login

In the above syntax, it is judged that the price adjustment you is a Boolean value. When the value is true, execute " {}", otherwise no processing will be performed. Among them, when there is only one statement in the code block, "{}" can be omitted.

2. if...else statement

if...else statement is also called a double branch statement. When a certain condition is met, a certain process is performed, otherwise another process is performed. A treatment. For example, to determine the age of a student, if he is 18 years or older, he is an adult. Otherwise a minor. The specific syntax and examples are as follows:

Syntax:

if(判断条件){
         代码段1;
}else{
         代码段2;
}
Copy after login

Example:

if($age>=18){
         echo'已成年';
}else{
         echo'未成年';
}
Copy after login

In the above syntax, when the judgment condition is true, code 1 is executed; when the condition is false When, code segment 2 is executed.

In addition, PHP also has a special operator: the ternary AND operator (also known as the ternary operator), which can also complete the function of if...else statements. Its syntax and examples are as follows .

Grammar:

条件表达式?表达式1:表达式2
Copy after login

Example:

echo $age>=18?'已成年': '未成年';
Copy after login

In the above syntax format, first find the value of the conditional expression. If it is true, return the execution result of expression 1 ;If the conditional expression evaluates to false. Then return the execution result of expression 2.

It is worth mentioning that when expression 1 is the same as the conditional expression, it can be abbreviated and the middle part can be omitted. For example, when the student's age $age is a natural number (>=0), the example is as follows:

Syntax:

条件表达式?: 表达式2
Copy after login

Example:

echo $age?:'还未出生';
Copy after login

3. if…elseif…else statement

if…elseif…else statement is called a multi-branch statement, which is used to perform different processing for different situations. For example, if a student's test scores are graded, a score of 90 to 100 is considered excellent, a score of 80 to 90 is considered good, a score of 70 to 80 is considered average, a score of 60 to 70 is considered passing, and a score of 60 to 70 is considered passing. Less than 60 is a failing grade. The specific syntax is as follows:

Grammar structure:

if(条件1){
         代码段1;
}elseif(条件2){
         代码段2;
}
……
elseif(条件n){
         代码段n;
}else{
         代码段n+1;
}
Copy after login

Example:

if($score>=90){
         echo'优秀';
} elseif($score>=80){
         echo'良好';
} elseif($score>=70){
         echo'中等';
} elseif($score>=60){
         echo'及格';
} else{
         echo'不及格';
}
Copy after login

In the above syntax, when the judgment condition 1 is true, code segment 1 is executed; otherwise, the judgment continues Condition 2, if true, executes code segment 2, and so on; if all conditions are false, executes code segment n 1.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What does the select structure statement in php mean?. 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