PHP For 循环

The three-part for loop, foreach over arrays, and the by-reference trap.

Beginner
Key Points`foreach` is the default for arrays; `for` suits known counts and index arithmetic.

for 的三段式

PHP
<?php
for ($i = 1; $i <= 3; $i++) {
    echo "第 $i 次\n";
}

// 递减
for ($i = 3; $i >= 1; $i--) {
    echo $i . ($i > 1 ? "," : "");
}
echo "\n";

// 多表达式
for ($i = 0, $j = 10; $i < $j; $i += 3, $j -= 3) {
    echo "i=$i j=$j\n";
}
Output
第 1 次
第 2 次
第 3 次
3,2,1
i=0 j=10
i=3 j=7

foreach 遍历数组

foreach walks arrays and objects without touching indexes — much safer than for.

PHP
<?php
$arr = ["a" => 1, "b" => 2, "c" => 3];

$pairs = [];
foreach ($arr as $k => $v) {
    $pairs[] = "$k=$v";
}
echo implode(", ", $pairs), "\n";

// 只取值
foreach ([10, 20, 30] as $v) {
    echo $v . " ";
}
echo "\n";
Output
a=1, b=2, c=3
10 20 30

Modifying during iteration

PHP
<?php
// 引用遍历:直接改原数组
$nums = [1, 2, 3];
foreach ($nums as &$n) {
    $n *= 2;
}
unset($n);            // 必须解除引用!
print_r($nums);
Output
Array
(
    [0] => 2
    [1] => 4
    [2] => 6
)
**unset($n) is mandatory.** After the loop $n still references the last element; reusing $n silently overwrites the array tail — an extremely subtle bug.
PHP
<?php
$nums = [1, 2, 3];

foreach ($nums as &$n) {
    $n *= 2;
}
// 忘了 unset

$n = 999;             // 本意是给普通变量赋值
print_r($nums);
Output
Array
(
    [0] => 2
    [1] => 4
    [2] => 999
)

The last element became 999. This is nasty to debug, so many teams ban by-reference iteration in favour of array_map.

PHP
<?php
$nums = [1, 2, 3];
$doubled = array_map(fn($n) => $n * 2, $nums);
print_r($doubled);
print_r($nums);
Output
Array
(
    [0] => 2
    [1] => 4
    [2] => 6
)
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
)

for or foreach

SituationUse
遍历整个数组foreach
需要下标做运算(如取相邻元素)for + count
只需循环 N 次,与数组无关for
数组可能被 unset 留下空洞foreach
需要倒序遍历for 从 count-1 开始,或 array_reverse

for ($i = 0; $i < count($arr); $i++) calls count() each iteration. Hoist it: for ($i = 0, $len = count($arr); $i < $len; $i++).

Generators: for large data

Iterating hundreds of thousands of rows loads the whole array. A yield generator produces values on demand with constant memory.

PHP
<?php
function lines(int $n): Generator {
    for ($i = 1; $i <= $n; $i++) {
        yield $i => $i * $i;
    }
}

$sum = 0;
foreach (lines(5) as $i => $sq) {
    echo "$i^2=$sq\n";
    $sum += $sq;
}
echo "合计 $sum\n";
Output
1^2=1
2^2=4
3^2=9
4^2=16
5^2=25
合计 55

Friends & Partners