Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:115725 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 1043 invoked from network); 14 Aug 2021 14:46:58 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 14 Aug 2021 14:46:58 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 872FA1804B3 for ; Sat, 14 Aug 2021 08:18:25 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-1.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM,HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sat, 14 Aug 2021 08:18:25 -0700 (PDT) Received: by mail-pl1-f179.google.com with SMTP id c17so10428289plz.2 for ; Sat, 14 Aug 2021 08:18:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to; bh=eW0E1BeXNAHaCo2Dz7JL4Dcz/D79lFYOKqTtga2Z1/Q=; b=FwYicItJLsZyFdnlv4diVPqsTqdBZFv9jX2JC15f9RstuoBTu2Ndcsi+2VcZB5KA2d WsnMMYowI14vHY1DHomYkxui8U3pTP39PQUYa9bJ/HppYVhRO3lpkpifdTU5B+4GwoFC MausvMd63X1frt0qxhTE807FLJDNtc+t1v9vREDlU9x6q0zQAhz3MoErhnJz6cvAuDzo LV+Nqi3/MaGj7nGbSDX70lMm0/K43Q3wGZjRA+MOtLW2AiFox6BC7wXL+ahfoROYYbcs 7Gp2BdNArT9qqrmuYe8O7PvRedNWxOl9DiWbZvZbDDDqt+4RpeS71FNmvg4vj+Nx2BI2 MRKw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to; bh=eW0E1BeXNAHaCo2Dz7JL4Dcz/D79lFYOKqTtga2Z1/Q=; b=HHWRizGvRtJ/NhS27OIsa16a9CXV4CfGa2D6J16I/OMtxEpFuwHgl3Z0EeMe707S74 /PoL41/t8sqCgh3apJ5dq2TVeJ0M4RUyjV4NlIyMHuz8QHIzhQP+C4fvOmXRvXuU887d uMvfEGC2JDADpLYtWYET6fWQ948p7q2GX5BBgyuSE4SAcPTCf+LoNjvKvfcxLo0bCrFo tIbzJ4kWanXKRuYaC585+NVkG2/EU6Juz88LzbuFFPTSjaU3o5xtJrI6fHRbBrWR9QZb /OZu6vIZGumb8pHUBKUp1ayYw+9EEN9rS9AWxvVmpEqYCOtF/IMtht4N5yuWpQuO5KGo 9/LQ== X-Gm-Message-State: AOAM532/ANnvm04GIBczMqt7CtHTGe8GO+o0wJNXHJCSxp57NpdEsEMV Y2Rq9QxNIQunYs7CRVm2Wrc1Kdkvl87awc2jaDYTz9c/dVW+/w== X-Google-Smtp-Source: ABdhPJx3ZSvjUPhQPik5AkQmY9/KvuqBLe3iaSaJaxMVcm3RJoxf+BDNIwqcz4p+DALV963/To3HU5wK6pcSWQ4JFdk= X-Received: by 2002:a17:90a:9af:: with SMTP id 44mr7800528pjo.62.1628954302286; Sat, 14 Aug 2021 08:18:22 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Sat, 14 Aug 2021 17:17:45 +0200 Message-ID: To: PHP internals Content-Type: multipart/alternative; boundary="0000000000008b0e4405c9867a7a" Subject: Re: [PHP-DEV] Unwrap reference after foreach From: divinity76@gmail.com (Hans Henrik Bergan) --0000000000008b0e4405c9867a7a Content-Type: text/plain; charset="UTF-8" well today you can do foreach($it as &$value){...} unset($value); - which is pretty close, but it will break with $value="initial";foreach($it as &$value){...}unset($value); echo $value; here $value will not be "initial", it will be undefined, however you *CAN* do $value="initial";(function()use(&$it){foreach($it as &$value){...}unset($value);})(); echo $value; and it will still contain "initial" and here the unset is even unnecessary, but yeah.. usually people do not go to such lengths to get a contained variable scope ^^ On Sat, 14 Aug 2021 at 15:23, Hossein Baghayi wrote: > On Fri, 13 Aug 2021 at 17:59, Nikita Popov wrote: > > > I'd like to address a common footgun when using foreach by reference: > > https://wiki.php.net/rfc/foreach_unwrap_ref > > > > Hello, > I had a question regarding this. > Wouldn't it be possible to limit ```$value```'s scope to only foreach's > block then discard it? Unless it was already defined elsewhere. > > ``` > foreach($nice_stuff as &$value) {} //we are done here and no need to keep > value around. > > echo $value; // $value is Undefined here. > > $value = null; > foreach($stuff as &$value) {} //we can keep the value here since it doesn't > belong to foreach. > > echo $value; // prints some fancy pancy stuff > ``` > --0000000000008b0e4405c9867a7a--