Foreach döngüsünde bir işlevi kullanarak sonucunu önbelleğe alır, ya da işlevini her zaman çağırır mı?

1 Cevap

Aşağıdaki kodu:

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?

1 Cevap

Hayır, test kesin değildir.

Herhangi bir kereden fazla ilk ifadeyi değerlendirmek için hiçbir mantıklı. Bir foreach döngü temel dayanak noktası.

A for döngü üç argüman vardır, ve bu ikinci ve üçüncü her yineleme değerlendiriyor.