PHP:将不止一次调用foreach体中的操作?

she*_*iel 3 php

foreach(functionWithComplicatedComputationReturningAnArray()as $ key => $ value)

以上代码会多次调用该函数吗?

一般来说,在这方面是否有规则,关于接受函数调用作为参数的PHP语言结构?

谢谢

吉迪

cle*_*tus 8

不,它会被召唤一次.这个:

foreach (functionWithComplicatedComputationReturningAnArray() as $key => $value) {
  ...
}
Run Code Online (Sandbox Code Playgroud)

在语义上等同于:

$tmp = functionWithComplicatedComputationReturningAnArray();
foreach ($tmp as $key => $value) {
  ...
}
Run Code Online (Sandbox Code Playgroud)

(忽略第二个将泄漏foreach循环外的值的事实).