Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:122084 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 45342 invoked from network); 2 Jan 2024 14:45:08 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 2 Jan 2024 14:45:08 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1704206738; bh=3XzSftWax4JffR1rzDxt9lYV5D9xbd/Mz4EM11CEoB8=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=a7EpKKtFuy5rxzka2pQec1JA5hoV8W+UqPWF6BJiYUK2L7T2sFqssPh5SelucC5Wq oNaYQYJzfLG0/bvS2JygOa0qOemMA7ig1oEQ2LjIWXF6bLGTWbD12ed75zsYdrUxdF Gh0uYFSp8osVrQMnfnqhvbhGWVvZ49JWVTHit1Z5ZQjcgfLUoK3AVmDdsTwXsGRHdl Sy2addR1JXtvsi5m9wPdLel09Z7l5o7hKliO5lbpov1wsHUJR4oKYqEi5pHgogrfKC 3Pu4DfDApp1mWlhy/KWQJ1pawULu9YLY4nAJWYrqc6SNDFyB45fFcyUdKSm4FSln/j NYmOUsM8SWOIg== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 5EDAA180058 for ; Tue, 2 Jan 2024 06:45:38 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-1.4 required=5.0 tests=BAYES_05,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS, FREEMAIL_ENVFROM_END_DIGIT,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from mail-lj1-f180.google.com (mail-lj1-f180.google.com [209.85.208.180]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Tue, 2 Jan 2024 06:45:34 -0800 (PST) Received: by mail-lj1-f180.google.com with SMTP id 38308e7fff4ca-2ccbded5aa4so70102111fa.1 for ; Tue, 02 Jan 2024 06:45:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1704206703; x=1704811503; darn=lists.php.net; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=6hzLRq2BvuOyMZJ0OF3n/UXQtHnw5kp9kh5G7S2LI7g=; b=fVSa0PFGRFtsVYPLToT+pLaMTQMsYC15DN06u96CG34b50gRl/yJaXfzAusDf3afPh OkWmasK/Yo5904VCVO85lUMLJSl81+Ymn8gx4sIRP4UACwc6aa1RuHQmMhLx46Cx3X3m eJEeWtsXSc2uw+L1bD0NKRVzajZxUJD/ZOH4M8WY1j2SmW0Vom/+VgBPpJ1Zc/Ov8U63 8d+qAc4lddtOlPtvlO3J+ZolgfvdufLpssoDTIrLfidE4aPFxtoiumwv/r+piG/wiKyH i0OAz2gCaxnyJEi3/MqF/Nm61Rxdt+z3qNxVuptD30yZwJEKMlHIlov/cENJwhkWz8ZD WnlA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704206703; x=1704811503; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=6hzLRq2BvuOyMZJ0OF3n/UXQtHnw5kp9kh5G7S2LI7g=; b=GemCxP2WvqLvGvYkuqLpJIdd9gXv2OKxbndaQFrc+nqJSI1779mHxm/H0gOzg6LUFF 84AgzKEXzXpBDa2HkjggoWlrxU5VmRnuqWULBS1Y/EYaT81kLzo4mcAMbfYNuZ73Myu7 gVlr0kVPhZUwVGB5f6NwyTe20wf3ocjqOXL8LpVAPhS5rk5/RkOvUQgCoS3ypxopvWfs ch01FAOMn+Ka+pTPy9psKJ7alxvyQ/KifEc734YiCv3Q+DlcpwsJJdtlsLsy4ewDx0VP 9pYGBnEDMW9jsn3AuWIUr8HH9xcRvDMXL1oSYmYoiBB5AdoImMKtiGXOBrU25s90iQOc 6XQA== X-Gm-Message-State: AOJu0Yykb1fRvUR29/PAKicZ2N+Ulhis2vYtVrV0hq75uH1xD/RQaSBu pltd/10b3I92wM/KLUUj1KMKKcB3CRaifXFOWac= X-Google-Smtp-Source: AGHT+IF3eyog4zx/HYDyySBvB0APIcZcdnb4wGclzzo6P9FmDgAxMxNF7qY8GLlNBlcCAGj5rcqxo/QzBCZu+Rf2hZM= X-Received: by 2002:a05:651c:4c6:b0:2cd:dfe:74bf with SMTP id e6-20020a05651c04c600b002cd0dfe74bfmr748119lji.5.1704206702930; Tue, 02 Jan 2024 06:45:02 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Tue, 2 Jan 2024 15:44:51 +0100 Message-ID: To: mickmackusa Cc: internals Content-Type: text/plain; charset="UTF-8" Subject: Re: [PHP-DEV] Declaring new elements as references while destructuring within a foreach() head From: tekiela246@gmail.com (Kamil Tekiela) I don't know if this was designed this way purposefully, but it makes sense to me. I see nothing out of the ordinary here. It makes for some really nasty code, but that can probably be said about all code that uses references. If we try to simplify your example, we can see more clearly what's happening. First, let's take the loop out of the equation. $arr = []; ['foo' => &$v] = $arr; $v = 1; var_dump($arr); This creates an array with one element whose value is 1 and key is foo. That array restructuring is pretty much just a syntactic sugar for an assignment operation. So we can simplify this example further by replacing the array destructuring with this: $v = & $arr['foo']; Now, this looks like an average assignment of reference in PHP. The rules of assignment-by-ref state: > If you assign, pass, or return an undefined variable by reference, it will get created. https://www.php.net/manual/en/language.references.whatdo.php#language.references.whatdo.assign Since array elements have similar logic to plain variables, this applies to undefined keys, too. The foo key in the example above is created initially with a NULL value. Using the variable alias we created, we can assign a different value to it. Coming back to your example from SO, the array destructuring in foreach is just a convoluted way of assignment-by-ref. After simplifying it, it becomes: foreach ($array as $key => $row) { $x = & $array[$key]['new']; $x = $row['bar']; } Which looks pretty normal to me. I would say it's definitely not a bug. It's just a mix of two PHP features that together make up for some confusing-looking code.