Home Backend Development PHP Problem PHP has several judgment statements

PHP has several judgment statements

Jun 13, 2022 pm 02:40 PM
php

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:

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$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;;
}
?>
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

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$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;
}
?>
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”。

示例:

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$a = 10;
$a % 2 == 0 ? print &#39;$a 是偶数!&#39; : print &#39;$a 是奇数!&#39;;
?>
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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1667
14
PHP Tutorial
1273
29
C# Tutorial
1255
24
PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

See all articles