Does using a function in foreach loop caches the result, or calls the function each time?

后端 未结 1 1619
你的背包
你的背包 2021-02-12 10:14

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 cal

相关标签:
1条回答
  • 2021-02-12 10: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.

    0 讨论(0)
提交回复
热议问题