How to write php judgment statement
Writing method: 1. "if (condition) {statement block;} else if (condition) {statement block;} else {statement block;}"; 2. "switch (expression) {case value 1 :Statement block;break;...case value n:Statement block;break;default:Statement block;}".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
In the process of program development, it is often necessary to In this case, different codes are executed. At this time, conditional judgment is needed. In PHP, if or switch can be used for conditional judgment. The corresponding conditional statement should be selected according to the actual scenario:
1. if else statement
The if else statement is a type of execution based on conditional judgment in process control. When this statement is executed, the condition is first judged, and then the corresponding operation is made based on the judgment result. It can be subdivided into three types, namely if
statement, if...else
statement, if...else if...else
statement .
1), if conditional statement
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 the statement block that needs to be executed contains more than one statement, you must use { } to enclose them to represent a whole; if there is only one statement to be executed, you can omit the curly brackets mark. Like the following:
if (判断条件) 一条语句;
The execution flow of the if statement is shown in the figure below:
[Example] Define an array and use the if statement to determine whether the number is It is not an even number. The code is as follows:
<?php $num = 34; if ($num % 2 == 0) { echo '$num ='.$num.', 是偶数!'; } ?>
The running result is as follows:
$num =34, 是偶数!
2), if...else...conditional statement
## The #if statement can only perform operations when the judgment result is true, which is not enough in many cases, 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; }
if (判断条件) 语句块 1; else 语句块 2;
<?php $num = rand(1,31); //生成一个 1~31 之间的随机数 if ($num % 2 == 0) { echo '$num ='.$num.', 是偶数!'; } else { echo '$num ='.$num.', 是奇数!'; } ?>
$num =27, 是奇数!
3), if...elseif...else...conditional statement
The else if statement is the same as the else statement. It extends the if statement. The else 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; }
<?php $score = 89; if ($score > 90) { echo '成绩的级别为:优!'; } else if ($score > 70) { echo '成绩的级别为:良!'; } else if ($score > 60) { echo '成绩的级别为:中!'; } else { echo '成绩的级别为:差!'; } ?>
成绩的级别为:良!
2. switch...case...conditional 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. Clearly. 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; }
switch 语句根据表达式的值,依次与 case 中的值进行比较,如果不相等,继续查找下一个 case;如果相等,就会执行对应的语句,直到 switch 语句结束或遇到 break 为止。
一般来说,switch 语句最终都有一个默认值 default,如果在前面的 case 中没有找到相符的条件,则执行默认语句,和 else 语句类似。
switch 语句的执行流程如下图所示:
在使用 switch 语句时应该注意以下几点:
和 if 语句不同的是,switch 语句后面表达式的数据类型只能是整型或字符串,不能是 bool 型。通常这个表达式是一个变量名称,虽然 PHP 是弱类型语言,在 switch 后面表达式的变量可以是任意类型数据,但为了保证匹配执行的准确性,最好只使用整型或字符串中的一种类型。
和 if 语句不同的是,switch 语句后面的花括号是必须有的。
case 语句的个数没有规定,可以无限增加。但 case 标签和 case 标签后面的值之间应该有一个空格,值后面必须有一个冒号,这是语法的一部分。
switch 匹配完成以后,将依次逐条执行匹配的分支模块中的语句,直到 switch 结构结束或者遇到了 break 语句才停止执行。所以,如果一个分支语句的后面没有写上 break 语句,程序将会继续执行下一个分支语句的内容。
与 if 语句中的 else 类似,switch 语句中 default 标签直接在后面加上一个冒号,看似没有条件,其实是有条件的,条件就是“表达式”的值不能与前面任何一个 case 标签后的值相等,这时才执行 default 分支中的语句。default 标签和 if 中的 else 子句一样,它不是 switch 语句中必需的,可以省略。
【示例】使用 date() 函数获取当前星期的英文缩写,根据缩写打印今天是星期几,代码如下所示:
<?php $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; } ?>
运行结果如下:
星期四
推荐学习:《PHP视频教程》
The above is the detailed content of How to write php judgment statement. For more information, please follow other related articles on the PHP Chinese website!

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

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

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.
