PHP循环主要有四种:while,do…while,for,foreach。下面我们分开讲解每种循环的用法
PHP中的循环主要用户执行相同代码块运行指定的次数。
PHP循环主要有四种:while,do…while,for,foreach。下面我们分开讲解每种循环的用法。
while语句:
只要指定的条件成立,则循环执行代码块。
格式:
while(expr)
{
statement;
}
语义:首先判断expr,如果表达式expr为假则结束;如果表达式expr为真,执行statement语句,执行完毕再次判断expr,若表达式expr仍为真,则继续执行statement语句;直至表达式expr为假,结束。
例:
复制代码 代码如下:
$i=1;
while($iecho $i;
$i++;
}
?>
上面的例子示范了一个循环,只要变量 i 小于或等于 5,代码就会一直循环执行下去。循环每循环一次,变量就会递增 1,然后输入i的值;
do…while 语句:
首先执行一次代码块,然后在指定的条件成立时重复这个循环.
格式:
do{
statement;
}
while(expr)语义:首先执行一次statement语句,然后再判断expr,如果表达式expr为假则结束;如果表达式expr为真,则继续循环执行statement语句,执行完毕再次判断expr,若表达式expr仍为真,则继续执行statement语句;直至表达式expr为假,,结束。
注意:他和while的区别在于,do…while第一次不进行任何判断就执行一次语句,然后再进行判断条件是否成立,这里要注意,其他和while是一样的。
例子:
复制代码 代码如下:
$i=6;
do
{
$i++;
echo "The number is " . $i . "
";
}
while ($i?>
上面的例子示范了一个循环,我给变量i赋值6,这里很显然和while中的条件相比较是不成立的,但结果输入6,也就是之前说的do…while语句,他第一次没进行任何判断,先执行一次,然后再判断条件是否成立。
for 语句
:如果您已经确定了代码块的重复执行次数,则可以使用 for 语句。
语法
for (initialization; condition; increment)
{
code to be executed;
}
语义:for 语句有三个参数。第一个参数初始化变量,第二个参数保存条件,第三个参数包含执行循环所需的增量。如果 initialization 或 increment 参数中包括了多个变量,需要用逗号进行分隔。而条件必须计算为 true 或者 false。
例子:
下面的例子会把文本 “Hello World!” 显示 5 次:
复制代码 代码如下:
for ($i=1; $i{
echo "Hello World!
";
}
?>
foreach 语句:foreach 语句用于循环遍历数组。
每进行一次循环,当前数组元素的值就会被赋值给 value 变量(数组指针会逐一地移动) – 以此类推。
语法
foreach (array as value)
{
code to be executed;
}
例子
下面的例子示范了一个循环,这个循环可以输出给定数组的值:
复制代码 代码如下:
$arr=array("one", "two", "three");
foreach ($arr as $value)
{
echo "Value: " . $value . "
";
}
?>
释义:首先我定义一个数组arr,然后我使用foreach来循环,其中($arr as $value)的意思是,把$arr数组中的值赋给$value,然后执行语句是输出$value的值。
输出的结果为:
one
two
three

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

SublimeText3 Linux new version
SublimeText3 Linux latest version

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor

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

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