Home php教程 php手册 总结各种不同PHP控制语句

总结各种不同PHP控制语句

Jun 13, 2016 am 11:11 AM
php different it Summarize control yes condition Features statement language important

PHP控制语句1、IF语句

IF语句是多数语言中的一个重要特点,它根据条件执行程序段。PHP的IF语句类似于 C:

  1. if (expr)   
  2. statement 

正如在表达式中所论述, expr 被计算为它的真值。如果 expr 为TRUE, PHP执行相应语句, 如果为FALSE 则忽略它。
如果$a 大于 $b,下例将显示 ’a is bigger than b’:

  1. if ($a >$b)   
  2. print "a is bigger than b"; 

通常,你希望根据条件执行多于一条语句。当然,不需要给每条语句都加上 IF 判断。取而代之,可以把多条语句组成一个语句组。 

If语句可以嵌套于其他 IF语句中,使你能够灵活地有条件的执行程序的各个部分。

PHP控制语句2、 ELSE语句

通常你希望满足特定条件时执行一条语句,不满足条件是执行另一条语句。ELSE就是用来做这个的。ELSE 扩展IF语句,在IF语句表达式为 FALSE时执行另一条语句。例如, 下面程序执行如果 $a 大于 $b则显示 ’a is bigger than b’,否则显示 ’a is

<ol class="dp-xml">
<li class="alt"><span><span>NOT bigger than b’:   </span></span></li>
<li>
<span>if ($a</span><span class="tag">></span><span>$b) {   </span>
</li>
<li class="alt"><span>print "a is bigger than b";   </span></li>
<li><span>}   </span></li>
<li class="alt"><span>else {   </span></li>
<li><span>print "a is NOT bigger than b";   </span></li>
<li class="alt"><span>}  </span></li>
</ol>
Copy after login

PHP控制语句3、 ELSEIF语句

ELSEIF,就象名字所示,是IF和ELSE的组合,类似于 ELSE,它扩展 IF 语句在IF表达式为 FALSE时执行其他的语句。但与ELSE不同,它只在ELSEIF表达式也为TRUE时执行其他语句。

可以在一条IF语句中使用多条ELSEIF语句。第一个ELSEIF表达式为TRUE的语句将被执行。在PHP 3中,你也可以写成 ’else if’ (写成两个单词)和 ’elseif’ (写成一个单词)效果一样。这只是写法上的细小差别(如果你熟悉 C,它也是),结果是完全一样的。
ELSEIF语句仅在IF表达式和任何前面的ELSEIF表达式都为FALSE,且当前ELSEIF表达式为TRUE时执行。
下面是一个含有ELSEIF和ELSE的嵌套格式的IF语句:

<ol class="dp-xml">
<li class="alt"><span><span>if ($</span><span class="attribute">a</span><span>==5):   </span></span></li>
<li><span>print "a equals 5";   </span></li>
<li class="alt"><span>print "...";   </span></li>
<li>
<span>elseif ($</span><span class="attribute">a</span><span>==6):   </span>
</li>
<li class="alt"><span>print "a equals 6";   </span></li>
<li><span>print "!!!";   </span></li>
<li class="alt"><span>else:   </span></li>
<li><span>print "a is neither 5 nor 6";   </span></li>
<li class="alt"><span>endif;  </span></li>
</ol>
Copy after login

PHP控制语句4、 WHILE语句

WHILE循环是PHP 3的一种简单的循环。象在 C 中一样。WHILE语句的基本格式是:

WHILE(expr) statement

WHILE语句的意思非常简单。它告诉PHP只要WHILE表达式为TRUE就重复执行嵌套的语句。每次循环开始时检查WHILE表达式的值,所以即使在嵌套语句内改变了它的值,本次执行也不会终止,而直到循环结束(每次PHP运行嵌套的语句称为一次循环)。类似于IF语句,你可以用大括号把一组语句括起来,在同一个WHILE循环中执行多条语句:

WHILE(expr): statement ... ENDWHILE;

下面例子完全相同, 都打出数字 1 到 10:

/* example 1 */

<ol class="dp-xml">
<li class="alt"><span><span>$</span><span class="attribute">i</span><span>=</span><span class="attribute-value">1</span><span>;   </span></span></li>
<li><span>while ($i0); </span></li>
</ol>
Copy after login

上面循环只执行一次, 因为第一次循环后,当检查真值表达式时, 它算出来是 FALSE ($i 不大于 0)循环执行终止。

PHP控制语句5、 FOR循环语句

FOR循环是PHP中最复杂的循环。象在 C 中一样。 FOR循环的语法是:
FOR (expr1; expr2; expr3) statement
第一个表达式(expr1)在循环开始时无条件的计算(执行)。
每一次循环, 表达式 expr2 都被计算。如果结果为 TRUE, 则循环和嵌套的语句继续执行。如果结果为 FALSE,则整个循环结束。 

每次循环结束时, expr3 被计算(执行). 每一个表达式都可为空。expr2 为空则循环的次数不定(PHP 默认它为TRUE,象C一样)。除非你要通过一个条件的 BREAK语句代替FOR 的真值表达式来结束循环,否则不要这样。
考虑下面例子。它们都显示数字 1 到 10:

/* example 1 */ 

<ol class="dp-xml">
<li class="alt"><span><span> for ($</span><span class="attribute">i</span><span>=</span><span class="attribute-value">1</span><span>; $i10) {   </span></span></li>
<li><span>break;   </span></li>
<li class="alt"><span>}   </span></li>
<li><span>print $i;   </span></li>
<li class="alt"><span>}   </span></li>
<li><span>/* example 3 */   </span></li>
<li class="alt">
<span>$</span><span class="attribute">i</span><span> = </span><span class="attribute-value">1</span><span>;   </span>
</li>
<li><span>for (;;) {   </span></li>
<li class="alt">
<span>if ($i </span><span class="tag">></span><span>10) {   </span>
</li>
<li><span>break;   </span></li>
<li class="alt"><span>}   </span></li>
<li><span>print $i;   </span></li>
<li class="alt"><span>$i++;   </span></li>
<li><span>}  </span></li>
</ol>
Copy after login

当然,第一个例子显然是最好的,但借此你可以发现在FOR 循环中很多场合可以使用空的表达式。
其他的语言有一条foreach语句用来遍历一个数组或哈希(hash)表。PHP使用while语句和 list()、each() 函数来达到这个功能。

PHP控制语句6、 SWITCH选择语句

SWITCH语句就象是对同一个表达式的一系列IF语句。在很多时侯,你想把同一个变量(或者表达式)和许多不同的值去比较 ,并根据不同的比较结果执行不同的程序段。这就是 SWITCH语句的用处了。
下面两个例子通过不同的方法做同一件事,一个用一组 IF语句,另外一个用 SWITCH 语句:

/* example 1 */

<ol class="dp-xml">
<li class="alt"><span><span>if ($</span><span class="attribute">i</span><span> == 0) {   </span></span></li>
<li><span>print "i equals 0";   </span></li>
<li class="alt"><span>}   </span></li>
<li>
<span>if ($</span><span class="attribute">i</span><span> == 1) {   </span>
</li>
<li class="alt"><span>print "i equals 1";   </span></li>
<li><span>}   </span></li>
<li class="alt">
<span>if ($</span><span class="attribute">i</span><span> == 2) {   </span>
</li>
<li><span>print "i equals 2";   </span></li>
<li class="alt"><span>}   </span></li>
<li><span>/* example 2 */   </span></li>
<li class="alt"><span>switch ($i) {   </span></li>
<li><span>case 0:   </span></li>
<li class="alt"><span>print "i equals 0";   </span></li>
<li><span>break;   </span></li>
<li class="alt"><span>case 1:   </span></li>
<li><span>print "i equals 1";   </span></li>
<li class="alt"><span>break;   </span></li>
<li><span>case 2:   </span></li>
<li class="alt"><span>print "i equals 2";   </span></li>
<li><span>break;   </span></li>
<li class="alt"><span>} </span></li>
</ol>
Copy after login


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 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)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

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,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

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

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

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

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

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 PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

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: 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.

See all articles