Php入门教程之PHP 循环语句使用方法
今天就是PHP基础知识的最后两篇文章,目的很简单就是让大家对PHP有个初步的认识,这篇主要说下PHP循环,它用于执行相同的代码块指定的次数。为我们的开发节省时间。
循环
在您编写代码时,您经常需要让相同的代码块运行很多次。您可以在代码中使用循环语句来完成这个任务。
在 PHP 中,我们可以使用下列循环语句:
while
只要指定的条件成立,则循环执行代码块
do…while
首先执行一次代码块,然后在指定的条件成立时重复这个循环
for
循环执行代码块指定的次数
foreach
根据数组中每个元素来循环代码块
while 语句
只要指定的条件成立,while 语句将重复执行代码块。
语法
while (condition)
code to be executed;
例子
下面的例子示范了一个循环,只要变量 i 小于或等于 5,代码就会一直循环执行下去。循环每循环一次,变量就会递增 1:
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy4870')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy4870><html>
<body>
<?php
$i=1;
while($i<=5)
{
echo "The number is " . $i . "<br />";
$i ;
}
?>
</body>
</html>
do…while 语句
do…while 语句会至少执行一次代码 – 然后,只要条件成立,就会重复进行循环。
语法
do
{
code to be executed;
}
while (condition);
例子
下面的例子将对 i 的值进行一次累加,然后,只要 i 小于 5 的条件成立,就会继续累加下去:
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy7198')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy7198><html>
<body>
<?php
$i=0;
do
{
$i ;
echo "The number is " . $i . "<br />";
}
while ($i<5);
?>
</body>
</html>
for 语句
如果您已经确定了代码块的重复执行次数,则可以使用 for 语句。
语法
for (initialization; condition; increment)
{
code to be executed;
4 }
注释:for 语句有三个参数。第一个参数初始化变量,第二个参数保存条件,第三个参数包含执行循环所需的增量。如果 initialization 或 increment 参数中包括了多个变量,需要用逗号进行分隔。而条件必须计算为 true 或者 false。
例子
下面的例子会把文本 “Hello World!” 显示 5 次:
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy1054')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy1054><html>
<body>
<?php
for ($i=1; $i<=5; $i )
{
echo "Hello World!<br />";
}
?>
</body>
</html>
foreach 语句
foreach 语句用于循环遍历数组。
每进行一次循环,当前数组元素的值就会被赋值给 value 变量(数组指针会逐一地移动) – 以此类推。
语法
foreach (array as value)
{
code to be executed;
}
例子
下面的例子示范了一个循环,这个循环可以输出给定数组的值:
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy2626')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy2626><html>
<body>
<?php
$arr=array("one", "two", "three");
foreach ($arr as $value)
{
echo "Value: " . $value . "<br />";
}
?>
</body>
</html>
一个遍历目录函数
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy1079')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy1079><?php/*
* 递归获取指定路径下的所有文件或匹配指定正则的文件(不包括“.”和“..”),结果以数组形式返回
* @param string $dir
* @param string [$pattern]
* @return array
*/
function file_list($dir,$pattern="")
{
$arr=array();
$dir_handle=opendir($dir);
if($dir_handle)
{
// 这里必须严格比较,因为返回的文件名可能是“0”
while(($file=readdir($dir_handle))!==false)
{
if($file==='.' || $file==='..')
{
continue;
}
$tmp=realpath($dir.'/'.$file);
if(is_dir($tmp))
{
$retArr=file_list($tmp,$pattern);
if(!emptyempty($retArr))
{
$arr[]=$retArr;
}
}
else
{
if($pattern==="" || preg_match($pattern,$tmp))
{
$arr[]=$tmp;
}
}
}
closedir($dir_handle);
}
return $arr;
}
// 列出网站根目录下所有以".php"扩展名(不区分大小写)结尾的文件
echo '<pre>';
print_r(file_list($_SERVER['DOCUMENT_ROOT'],"//.php$/i"));
echo '</pre>';
?>
这个就可以方便的无限目录遍历了。.
foreach与while性能比较
在循环里进行的是数组“读”操作,则foreach比while快:
在循环里进行的是数组“写”操作,则while比foreach快
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy1932')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy1932>/**
* while跟foreach函数性能比较
*
*/
// foreach 函数
foreach ($array as $key => $value) {
echo $array[$key] = $value . '...';
}
// while 函数
while (list($key) = each($array)) {
$array[$key] = $array[$key] . '...';
}
总结:通常认为,foreach涉及到值复制,一定会比while慢,但实际上,如果仅仅是在循环里进行数组的读操作,那么foreach是很
快的,这是因为PHP采用的复制机制是“引用计数,写时复制”,也就是说,即便在PHP里复制一个变量,最初的形式从根本上说其实
仍然是引用的形式,只有当变量的内容发生变化时,才会出现真正的复制,之所以这么做是出于节省内存消耗得目的,同时也提升了
复制的效率。
这样看来,foreach的高效读操作就不难理解了。另外,既然foreach不适合处理数组写操作,那么我们可以得出一个结
论,多数情况下,类似foreach ($array as $key => $value)形式的做数组写操作得代码都应该被替换成while (list($key) =
each($array))。这些技巧产生的速度差异在小项目里可能并不明显,但是在类似框架这样的大项目中,一次请求动辄便会涉及到几
百几千几万次数组循环操作,差异就会明显放大。
您可能感兴趣的文章:
php入门教程(索引)
php入门基础教程之php循环语句
适合入门的php基础系列教程
php入门基础教程之php流程控制
for循环语句的入门教程
php循环语句控制break与continue的用法举例
jquery $.each 和for怎么跳出循环终止本次循环
PHP 循环语句基本语法结构笔记
php中 continue break exit return 的区别
php遍历数组的几种方法(for foreach list each while)