Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:99345 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 68664 invoked from network); 3 Jun 2017 16:21:17 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 3 Jun 2017 16:21:17 -0000 Authentication-Results: pb1.pair.com header.from=johannes@schlueters.de; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=johannes@schlueters.de; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain schlueters.de from 84.19.169.162 cause and error) X-PHP-List-Original-Sender: johannes@schlueters.de X-Host-Fingerprint: 84.19.169.162 unknown Received: from [84.19.169.162] ([84.19.169.162:59314] helo=mail.experimentalworks.net) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 1F/11-12681-5F1E2395 for ; Sat, 03 Jun 2017 12:21:10 -0400 Received: from kuechenschabe.fritz.box (ppp-46-244-185-86.dynamic.mnet-online.de [46.244.185.86]) by mail.experimentalworks.net (Postfix) with ESMTPSA id 1F8F5562AA; Sat, 3 Jun 2017 18:21:06 +0200 (CEST) Message-ID: <1496506865.4363.81.camel@schlueters.de> To: Michael Morris , "internals@lists.php.net" Date: Sat, 03 Jun 2017 18:21:05 +0200 In-Reply-To: References: <1496415412.4363.66.camel@schlueters.de> Content-Type: text/plain; charset="UTF-8" X-Mailer: Evolution 3.18.5.2-0ubuntu3.1 Mime-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: Re: [PHP-DEV] RFC proposal From: johannes@schlueters.de (Johannes =?ISO-8859-1?Q?Schl=FCter?=) 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