Home php教程 php手册 PHP 循环控制语句几种方法详解

PHP 循环控制语句几种方法详解

Jun 13, 2016 am 09:51 AM
for php while generally use several kinds exist cycle control method yes Detailed explanation statement

在php中循环语句一般是使用while,for,foreach而控制语句就是if swicth这些了,下面我来给大家介绍一下php循环控制语句用法吧。

1、if..else循环有三种结构第一种是只有用到if条件,当作单纯的判断。

解释成"若发生了某事则怎样处理"。
语法如下:if(expr){statement}其中的expr为判断的条件,通常都是用逻辑运算符号当判断的条件。
而statement为符合条件的执行部分程序,若程序只有一行,可以省略大括号{}。
范例:本例省略大括号。

 代码如下 复制代码
if($state==1)
echo"哈哈";
?>

这里特别注意的是,判断是否相等是==而不是=,ASP程序员可能常犯这个错误,=是赋值。
范例:本例的执行部分有三行,不可省略大括号。

 代码如下 复制代码
if($state==1){
echo"哈哈;
echo"
";
}
?>

第两种是除了if之外,加上了else的条件,可解释成"若发生了某事则怎样处理,否则该如何解决"。
语法如下:

 代码如下 复制代码
if(expr){
statement1
}else{
statement2
}

范例:上面的例子来修改成更完整的处理。
其中的else由于只有一行执行的指令,因此不用加上大括号。

 代码如下 复制代码
if($state==1){
echo"哈哈";
echo"
";
}else{
echo"呵呵";
echo"
";
}
?>

第三种就是递归的if..else循环,通常用在多种决策判断时。
它将数个if..else拿来合并运用处理。
直接看下面的例子

 代码如下 复制代码
if($a>$b){
echo"a比b大";
}elseif($a==$b){
echo"a等于b";
}else{
echo"a比b小";
}
?>

上例只用二层的if..else循环,用来比较a和b两个变量。
实际要使用这种递归if..else循环时,请小心使用,因为太多层的循环容易使设计的逻辑出问题,或者少打了大括号等,都会造成程序出现莫名其妙的问题。
2、for循环就单纯只有一种,没有变化,它的语法如下
for(expr1;expr2;expr3){statement}
其中的expr1为条件的初始值。
expr2为判断的条件,通常都是用逻辑运算符号(logicaloperators)当判断的条件。
expr3为执行statement后要执行的部份,用来改变条件,供下次的循环判断,如加一..等等。
而statement为符合条件的执行部分程序,若程序只有一行,可以省略大括号{}。
下例是用for循环写的的例子。

 代码如下 复制代码
for($i=1;$i ){
echo"这是第".$i."次循环
";
}
?>

3、switch循环,通常处理复合式的条件判断,每个子条件,都是case指令部分。
在实作上若使用许多类似的if指令,可以将它综合成switch循环。语法如下

 代码如下 复制代码
switch(expr){
caseexpr1:
statement1;
break;
caseexpr2:
statement2;
break;
default:
statementN;
break;
}

其中的expr条件,通常为变量名称。
而case后的exprN,通常表示变量值。
冒号后则为符合该条件要执行的部分。
注意要用break跳离循环。

 代码如下 复制代码
switch(date("D")){
case"Mon":
echo"今天星期一";
break;
case"Tue":
echo"今天星期二";
break;
case"Wed":
echo"今天星期三";
break;
case"Thu":
echo"今天星期四";
break;
case"Fri":
echo"今天星期五";
break;
default:
echo"今天放假";
break;
}
?>

这里需要注意的是break;
别遗漏了,default,省略是可以的。
很明显的,上述的例子用if循环就很麻烦了。
当然在设计时,要将出现机率最大的条件放在最前面,最少出现的条件放在最后面,可以增加程序的执行效率。
上例由于每天出现的机率相同,所以不用注意条件的顺序。

php循环控制语句
1、While语句

 代码如下 复制代码
While循环是php中最简单的循环语句,他的语法格式是:
While (expression){
statement;
}

当表达式expression的值为真时,将执行statement语句,执行结束后,再返回到expression表达式继续进行判断。直到表达式的值为假时,才跳出循环。
实例:

 代码如下 复制代码
$num = 1;
$str = “10以内的偶数为:”;
while($num if($num % 2 == 0){
$str.=$num.”";
}
$num++;
}
echo $str;
?>

2、Do…While语句
While语句还有一种形式的表示,Do…While.语法为:
Do{
statement;
}While(expression);
两者的区别在于:Do…While语句要比While语句多循环一次。
当While表达式的值为假时,While循环直接跳出当前循环,而Do…While语句则是先执行一遍程序块,然后再对表达式进行判断。
3、For语句

 代码如下 复制代码
For循环是php中最复杂的循环结构,它的语法格式为:
For(expression1;expression2;expression3){
statement;
}

其中:expression1在第一次循环时无条件取一次值。
expression2在每次循环开始前求值,如果值为真,则执行statement;否则跳出循环,继续往下执行。expression3为每次循环后被执行。
实例:

 代码如下 复制代码
$num=1;
for($i=1;$i $num *=$i;
}
echo $num;
?>

4、Foreach语句
Foreach循环是php4.0引进来的,只能用于数组。在php5中,又增加了对对象的支持。该语句的语法格式为:
foreach(array_expression as $value)
statement;

Foreach(array_expression as $key => $value)
statement;
Foreach语句将遍历数组array_expression,每次循环时,将当前数组中的值赋给$value(或是将数组下表赋给$key、对应的数组值赋给$value),同时,数组指针向后移动,如此反复循环,直到遍历结束。当使用Foreach语句时,数组指针将自动被重置,所以不需要手动设置指针位置。实例

 代码如下 复制代码
$arr=array(“We”,”are”,”the”,”best”,”team”,”!”);
if(is_array($arr) == true){
foreach($arr as $key => $value){
echo $key.”=”.$value.”
”;
}
}else{
echo”该变量不是数组,不能使用foreach语句”;
}
?>
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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
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)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

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

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

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

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles