On Fr, 2017-06-02 at 13:12 -0400, Michael Morris wrote: > What about foreach by reference, which is honestly the only time I > use referencing anymore... > > foreach ($array as $key => &$value) { > $value = someOp($value); > } > > Is this also bad?
If someOp() takes parameters by value this code disables copy-on-write and during the function call $value has to be duplicated. If the iteration would use values then copy-on-write can be effective and avoid copying. But for modification this indeed is a case where references might be useful, in the simplified example. foreach ($array as $key => &$value) { $value++; } is nicer and faster than (at least in my expectation, haven't measured) foreach ($array as $key => &$value) { $arry[$key] = $value+1; } Some might argue that using array_map() would be even nicer style, while then we have more function calls (even though internally cached lookups so they are a bit faster than direct function calls) For a larger and more realistic case the performance calculation becomes complicated, but often the extra copies cost more. But then again since PHP 7 for "simple types" a copy is relatively cheaper than before. (actually they don't have copy-on-write anymore, but references still need special treatment ...) johannes -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php