25

In the following code:

function a(){
    echo 'a';
    return array(1,2,3,4);
}

foreach(a() as $t){
    echo $t;    
}

We can see a() is called only once, and it seems the returned value is cached...
But then I got to see this debate (see comments on the question) am I missing something?

Community
  • 1
  • 1
Itay Moav -Malimovka
  • 52,579
  • 61
  • 190
  • 278

1 Answers1

32

No, your test is conclusive.

It makes no sense for it to evaluate the first expression any more than once. It's the basic premise of a foreach loop.

A for loop has three arguments, and it does evaluate the second and third each iteration.

gahooa
  • 131,293
  • 12
  • 98
  • 101