Search code examples
phparraysexpressionlanguage-construct

What does "array_expression" mean in php?


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?


Solution

  • 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){}