What does "array_expression" mean in php?

851 Views Asked by At

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

Trying to better understand the foreach loop. In the documentation above it states "The first form loops over the array given by array_expression."

What in the world is an array_expression?

2

There are 2 best solutions below

0
AbraCadaver On BEST ANSWER

An array_expression is any expression that results in an array. So these are expressions that are not arrays themselves but result in an array when evaluated:

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

Or:

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

Or:

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

foreach(Test::do_it() as $val){}
3
samayo On

From the php manual's descriptionpage

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".

So, this means array_expression is just a dumb dummy text to let you know that the foreach takes array function.

In this case,

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

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

would result:

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