Penggunaan fungsi FOR循环 pada PHP


循环执行代码块指定的次数,或者当指定的条件为真时循环执行代码块。

Show

for 循环

for 循环用于您预先知道脚本需要运行的次数的情况。

语法

for (初始值; 条件; 增量)
{
    要执行的代码;
}

参数:

  • 初始值:主要是初始化一个变量值,用于设置一个计数器(但可以是任何在循环的开始被执行一次的代码)。
  • 条件:循环执行的限制条件。如果为 TRUE,则循环继续。如果为 FALSE,则循环结束。
  • 增量:主要用于递增计数器(但可以是任何在循环的结束被执行的代码)。

注释:上面的初始值增量参数可为空,或者有多个表达式(用逗号分隔)。

实例

下面的实例定义一个初始值为 i=1 的循环。只要变量 i 小于或者等于 5,循环将继续运行。循环每运行一次,变量 i 就会递增 1:

实例

<?phpfor($i=1; $i<=5; $i++){echo"数字为 " . $i . PHP_EOL; }?>

输出:

数字为 1
数字为 2
数字为 3
数字为 4
数字为 5


foreach 循环

foreach 循环用于遍历数组。

语法

foreach ($array as $value)
{
    要执行代码;
}

每进行一次循环,当前数组元素的值就会被赋值给 $value 变量(数组指针会逐一地移动),在进行下一次循环时,您将看到数组中的下一个值。

foreach ($array as $key => $value)
{
    要执行代码;
}

每一次循环,当前数组元素的键与值就都会被赋值给 $key 和 $value 变量(数字指针会逐一地移动),在进行下一次循环时,你将看到数组中的下一个键与值。

实例

下面的实例演示了一个输出给定数组的值的循环:

实例

<?php $x=array("Google","Runoob","Taobao"); foreach($xas$value){echo$value . PHP_EOL; }?>

输出:

Google
Runoob
Taobao

下面的实例演示了一个输出给定数组键与值的循环:

实例

<?php$x=array(1=>"Google", 2=>"Runoob", 3=>"Taobao"); foreach($xas$key => $value){echo"key 为 " . $key . ",对应的 value 为 ". $value . PHP_EOL; } ?>

输出:

key  为 1,对应的 value 为 Google
key  为 2,对应的 value 为 Runoob
key  为 3,对应的 value 为 Taobao

(PHP 4, PHP 5, PHP 7, PHP 8)

for 循环是 PHP 中最复杂的循环结构。它的行为和 C 语言的相似。 for 循环的语法是:

for (expr1; expr2; expr3)
    statement

第一个表达式(expr1)在循环开始前无条件求值(并执行)一次。

expr2 在每次循环开始前求值。如果值为 true,则继续循环,执行嵌套的循环语句。如果值为 false,则终止循环。

expr3 在每次循环之后被求值(并执行)。

每个表达式都可以为空或包括逗号分隔的多个表达式。表达式 expr2 中,所有用逗号分隔的表达式都会计算,但只取最后一个结果。expr2 为空意味着将无限循环下去(和 C 一样,PHP 暗中认为其值为 true)。这可能不像想象中那样没有用,因为经常会希望用有条件的 break 语句来结束循环而不是用 for 的表达式真值判断。

考虑以下的例子,它们都显示数字 1 到 10:

<?php
/* 示例 1 */for ($i 1$i <= 10$i++) {
    echo 
$i;
}
/* 示例 2 */for ($i 1; ; $i++) {
    if (
$i 10) {
        break;
    }
    echo 
$i;
}
/* 示例 3 */$i 1;
for (;;) {
    if (
$i 10) {
        break;
    }
    echo 
$i;
    
$i++;
}
/* 示例 4 */for ($i 1$j 0$i <= 10$j += $i, print $i$i++);
?>

当然,第一个例子看上去最简洁(或者有人认为是第四个),但用户可能会发现在 for 循环中用空的表达式在很多场合下会很方便。

PHP 也支持用冒号的 for 循环的替代语法。

for (expr1; expr2; expr3):
    statement;
    ...
endfor;

有时经常需要像下面这样例子一样对数组进行遍历:

<?php
/*
 * 此数组将在遍历的过程中改变其中某些单元的值
 */
$people = Array(
        Array(
'name' => 'Kalle''salt' => 856412), 
        Array(
'name' => 'Pierre''salt' => 215863)
        );

for(

$i 0$i count($people); ++$i)
{
    
$people[$i]['salt'] = rand(000000999999);
}
?>

以上代码可能执行很慢,因为每次循环时都要计算一遍数组的长度。由于数组的长度始终不变,可以用一个中间变量来储存数组长度以优化而不是不停调用 count()

<?php
$people 
= Array(
        Array(
'name' => 'Kalle''salt' => 856412), 
        Array(
'name' => 'Pierre''salt' => 215863)
        );

for(

$i 0$size count($people); $i $size; ++$i)
{
    
$people[$i]['salt'] = rand(000000999999);
}
?>

matthiaz

10 years ago

Looping through letters is possible. I'm amazed at how few people know that.

for($col = 'R'; $col != 'AD'; $col++) {
    echo $col.' ';
}

returns: R S T U V W X Y Z AA AB AC

Take note that you can't use $col < 'AD'. It only works with !=
Very convenient when working with excel columns.

nzamani at cyberworldz dot de

21 years ago

The point about the speed in loops is, that the middle and the last expression are executed EVERY time it loops.
So you should try to take everything that doesn't change out of the loop.
Often you use a function to check the maximum of times it should loop. Like here:

<?php
for ($i = 0; $i <= somewhat_calcMax(); $i++) {
 
somewhat_doSomethingWith($i);
}
?>

Faster would be:

<?php
$maxI
= somewhat_calcMax();
for (
$i = 0; $i <= $maxI; $i++) {
 
somewhat_doSomethingWith($i);
}
?>

And here a little trick:

<?php
$maxI
= somewhat_calcMax();
for (
$i = 0; $i <= $maxI; somewhat_doSomethingWith($i++)) ;
?>

The $i gets changed after the copy for the function (post-increment).

Andrew

8 years ago

You can use strtotime with for loops to loop through dates

<?php
for ($date = strtotime("2014-01-01"); $date < strtotime("2014-02-01"); $date = strtotime("+1 day", $date)) {
    echo
date("Y-m-d", $date)."<br />";
}
?>

Warbo

8 years ago

Remember that for-loops don't always need to go 'forwards'. For example, let's say I have the following code:

<?php
for ($i = 0; $i < calculateLoopLength(); $i++) {
 
doSomethingWith($i);
}
>?

As

other comments have pointed out, if "calculateLoopLength" will keep giving back the same value, it can be moved outside the loop:

<?

php
$loopLength
= calculateLoopLength();
for (
$i=0; $i < $loopLength; $i++) {
 
doSomethingWith($i);
}
?>

However, if the order the looping doesn't matter (ie. each iteration is independent) then we don't need to use an extra variable either, we can just count down (ie. loop 'backwards') instead:

<?php
for ($i=calculateLoopLength(); $i > 0; $i--) {
 
doSomething($i);
}
?>

In fact, we can simplify this even more, since "$i > 0" is equivalent to "$i" (due to type casting):

<?php
for ($i=calculateLoopLength(); $i; $i--) {
 
doSomething($i);
}
?>

Finally, we can switch to a 'pre-decrement' instead of a 'post-decrement' to be slightly more efficient (see, for example, http://dfox.me/2011/04/php-most-common-mistakes-part-2-using-post-increment-instead-of-pre-increment/ ):

<?php
for ($i = calculateLoopLength(); $i; --$i) {
 
doSomething($i);
}
?>

In this case we could also replace the entire loop with a map, which might make your algorithm clearer (although this won't work if calculateLoopLength() == 0):

<?php
array_map
('doSomething',
         
range(0, calculateLoopLength() - 1));
?>

ju dot nk at email dot cz

4 years ago

Please note that following code is working:

for ($i=$reverse?($N-1):0; $reverse?($i>-1):($i<$N); $reverse?$i--:$i++) {

... your code here

}

(Using PHP 5.4.45)

dx at e-mogensen dot dk

5 years ago

Warning about using the function "strlen" i a for-loop:

This note should might be under the "strlen" manual page, but there is a better chance for more paying attention here (nevertheless I have made a short note over there allso).

A loop function that test for the string length at each iteration takes forever (possibly due to "strlen" searches for the C-style string terminator - a binary 0 - every time..

So loops like this, using  "strlen" in the for...

for  ($i = 0;  $i < strlen($crc); $i++) .....

Will benefit tremendously in speed by a short step that saves the string length once and use that in the loop.

$clen = strlen($crc);

for  ($i = 0;  $i < $clen ; $i++) .....

Note: as a real hard-core programmer You are aware, that this is only valid if you don't change the string content inside the loop (and hereby allso the length). If the change is only occationly , You could just refresh the length variable or else just live with a quite slow loop.

This "discovery" was made from using an example of 16 bit crc calculation over at the "crc32" function manual page, that do exactly that..

JustinB at harvest dot org

17 years ago

For those who are having issues with needing to evaluate multiple items in expression two, please note that it cannot be chained like expressions one and three can.  Although many have stated this fact, most have not stated that there is still a way to do this:

<?php
for($i = 0, $x = $nums['x_val'], $n = 15; ($i < 23 && $number != 24); $i++, $x + 5;) {
   
// Do Something with All Those Fun Numbers
}
?>

Philipp Trommler

9 years ago

Note, that, because the first line is executed everytime, it is not only slow to put a function there, it can also lead to problems like:

<?php

$array

= array(0 => "a", 1 => "b", 2 => "c", 3 => "d");

for(

$i = 0; $i < count($array); $i++){

echo

$array[$i];

unset(

$array[$i]);

}

?>

This will only output the half of the elements, because the array is becoming shorter everytime the for-expression counts it.

AoKMiKeY

8 years ago

As a note for people just starting out and wanting to know if you can do some thing like this...

<?php For( $a = 0; $a < 10; $a++ ) { ?>

//Random html elements you would like to duplicate.

<?php } ?>

Then yes you can. It works like a charm.

Vincenzo Raco

8 years ago

In this code:

<?php

    $array

= array(
       
'pop0',
       
'pop1',
       
'pop2',
       
'pop3',
       
'pop4',
       
'pop5',
       
'pop6',
       
'pop7',
       
'pop8'
   
);
    echo
"Tot Before: ".count($array)."<br><br>";
    for (
$i=0; $i<count($array); $i++) {
        if (
$i === 3) {
            unset(
$array[$i]);
        }
        echo
"Count: ".count($array). " - Position: ".$i."<br>";
    }
    echo
"<br> Tot After: ".count($array)."<br>";?>

The result is:

---

Tot Before: 9

Count: 9 - Position: 0
Count: 9 - Position: 1
Count: 9 - Position: 2
Count: 8 - Position: 3
Count: 8 - Position: 4
Count: 8 - Position: 5
Count: 8 - Position: 6
Count: 8 - Position: 7

Tot After: 8

---

The position 8 is skipped, because the "expr2" {{ $i<count($array) }} is evaluated again, for each cycle.

The solution is:

<?php

        $array

= array(
       
'pop0',
       
'pop1',
       
'pop2',
       
'pop3',
       
'pop4',
       
'pop5',
       
'pop6',
       
'pop7',
       
'pop8'
   
);
    echo
"Tot Before: ".count($array)."<br><br>";
   
$count = count($array);
    for (
$i=0; $i<$count; $i++) {
        if (
$i === 3) {
            unset(
$array[$i]);
        }
        echo
"Count: ".count($array). " - Position: ".$i."<br>";
    }
    echo
"<br> Tot After: ".count($array)."<br>";?>

Anonymous

2 years ago

If you want to do a for and increment with decimal numbers:
<?php
for($i=0; $i<=2; $i+=0.1)
     echo
$i;
?>
The code will never show 2 as expected, because of decimal imprecision if I remember well.
You will need to round:
<?php
for($i=0; round($i,1)<=2; $i+=0.1)
    echo
$i.",";
?>
This code correctly shows 0,0.1 .... 2.

user at host dot com

18 years ago

Also acceptable:

<?php
 
for($letter = ord('a'); $letter <= ord('z'); $letter++)
   print
chr($letter);
?>

ju dot nk at email dot cz

4 years ago

Please note that following code is working:

$reverse = TRUE;  //iteration direction switch

for ($i=$reverse?($N-1):0; $reverse?($i>-1):($i<$N); $reverse?$i--:$i++) {

... your code here

}

(Using PHP 5.4.45)

htroyo

6 years ago

when iterating a multidimentional array like this:
for ($i = 0; $i < $size_x; $i++) {
    for ($j = 0; $j < $size_y; $j++) {
        do_something($a[$i][$j]);
    }
}
it is faster to use $a[$i][$j] than using $a[$j][$i]
for ($i = 0; $i < $size_x; $i++) {
    for ($j = 0; $j < $size_y; $j++) {
        do_something($a[$j][$i]);
    }
}
if you know about how RAM works you understand why

Anonymous

8 years ago

You can also work with arrays. For example, say you want to generate an array of 12 unique 2-letter strings:

<?phpfor ($names = array(); count($names) < 12; $names = array_unique($names)) {
   
/**
     * we assume here we have some $faker object
     * which generates n-letter strings
     */
   
$names[] = $faker->word(2);
}
print_r($names);
?>

will print something like:

Array
(
    [0] => cc
    [1] => cb
    [2] => dd
    [3] => db
    [4] => bb
    [6] => cd
    [8] => aa
    [9] => ad
    [10] => ca
    [11] => ac
    [12] => dc
    [15] => ab
)

bishop

19 years ago

If you're already using the fastest algorithms you can find (on the order of O(1), O(n), or O(n log n)), and you're still worried about loop speed, unroll your loops using e.g., Duff's Device:

<?php
$n
= $ITERATIONS % 8;
while (
$n--) $val++;
$n = (int)($ITERATIONS / 8);
while (
$n--) {
   
$val++;
   
$val++;
   
$val++;
   
$val++;
   
$val++;
   
$val++;
   
$val++;
   
$val++;
}
?>

(This is a modified form of Duff's original device, because PHP doesn't understand the original's egregious syntax.)

That's algorithmically equivalent to the common form:

<?php
for ($i = 0; $i < $ITERATIONS; $i++) {
   
$val++;
}
?>

$val++ can be whatever operation you need to perform ITERATIONS number of times.

On my box, with no users, average run time across 100 samples with ITERATIONS = 10000000 (10 million) is:
Duff version:       7.9857 s
Obvious version: 27.608 s

mparsa1372 at gmail dot com

1 year ago

This example counts to 100 by tens:

<?php
for ($x = 0; $x <= 100; $x+=10) {
  echo
"The number is: $x <br>";
}
?>

epicxmoe at gmail dot com

5 years ago

Adding Letters from A to Z inside an array.

You should test it out.

<!DOCTYPE html>
<html>
<body>
<?php
$letter
= array();
for (
$letters = 'A'; $letters != 'AA'; $letters++)
{
   
array_push($letter, $letters);
}
echo
'<pre>' . var_export($letter, true) . '</pre>';
?>
</body>
</html>

epicxmoe at gmail dot com

5 years ago

I've tried to search for a results on internet for a basic array which contain letters A to Z inside

<!DOCTYPE html>
<html>
<body>
<?php
$letter
= array();
for (
$letters = 'A'; $letters != 'AA'; $letters++)
{
   
array_push($letter, $letters);
}
echo
'<pre>' . var_export($letter, true) . '</pre>';
?>
</body>
</html>