"array_expression"在php中是什么意思?

What does "array_expression" mean in php?

http://php.net/manual/en/control-structures.foreach.php

试图更好地理解 foreach 循环。在上面的文档中它指出 "The first form loops over the array given by array_expression."

array_expression 到底是什么?

来自 php manual 的描述页面

Expressions are the most important building blocks of PHP. In PHP, almost anything you write is an expression. The simplest yet most accurate way to define an expression is "anything that has a value".

所以,这意味着 array_expression 只是一个 dumb 虚拟文本,让您知道 foreach 采用数组函数。

在这种情况下,

$arr = array(1, 2, 3);

foreach ($arr as $value) {
   var_dump($value);
}

结果:

int(1) int(2) int(3)

array_expression 是生成数组的任何 expression。所以这些表达式本身不是数组,但在计算时会产生一个数组:

foreach(range(1, 5) as $val){}

或者:

foreach($array = range(1, 5) as $val){}

或者:

class Test {
    public static function do_it() {
        return range(1, 5);
    }
}

foreach(Test::do_it() as $val){}