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; }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
<?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!

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

WebStorm Mac version
Useful JavaScript development tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool