Example:
foreach ($veryFatArray as $key => $value) {
Will the foreach assign the value behind the $key by reference to $value, or will $value be a copy of what’s stored in the array? And if yes, how could I get an reference only? The array values store pretty big amounts of data so copying them is not really good.
Don’t try and second-guess the interpreter is the moral of the story here.
$valueis actually a copy but an efficient copy. PHP uses pass-by-value (for non-objects) but also uses copy-on-write. What this means is that if you write:Just declare your intent: that you want to use the array values. Let PHP sort it out after that. Basically only use references in the loop like this if you intend to change the array. Otherwise you’re sending the wrong message to someone else who reads your code.
Section 1 of Copy-on-Write in the PHP Language should tell you everything you ever wanted to know about PHP copy-on-write.