Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:129599 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by lists.php.net (Postfix) with ESMTPS id 8204C1A00BC for ; Fri, 12 Dec 2025 21:55:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1765576534; bh=dJltQExLNxBOUNVF02i2DAQUC/hqK5O40Wv09D7S7oQ=; h=Date:From:To:In-Reply-To:References:Subject:From; b=F9I77mR/NmCxuHYy2s0HHDr+VhlccWV5xRfjLYQ9AdhwDXspTQG28jbdiUZegmAyb FxFBETP6bZYVULDrnJySo38EeIVRYzeKXc+Pnzislw6wiQLMYAnK5DXOOoUSlHD0Z1 Zc9tCa2g62QH9KY3UpotapdJZTSmEnNAcsp+fbQcgHO4+S2AfspgYVMVrTY2Fo2e3c y2k0SjAq9Vl59/bsJ6xYedj1FZdtCf/j9GKIuMYZtVB2a/FoK+L5WrDshyRcCDpmc2 SrvLlLxb6UYqebDUVjmJgA2ZQaYo9YNnWsywjptl3q9fcuQHWx2wAQe0gIb+FvHlD3 Rp+AOiVduX2wA== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 6DEA818005B for ; Fri, 12 Dec 2025 21:55:33 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-0.1 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_MISSING,RCVD_IN_DNSWL_LOW, SPF_HELO_PASS,SPF_NONE autolearn=no autolearn_force=no version=4.0.1 X-Spam-Virus: No X-Envelope-From: Received: from fhigh-a1-smtp.messagingengine.com (fhigh-a1-smtp.messagingengine.com [103.168.172.152]) (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 ; Fri, 12 Dec 2025 21:55:33 +0000 (UTC) Received: from phl-compute-04.internal (phl-compute-04.internal [10.202.2.44]) by mailfhigh.phl.internal (Postfix) with ESMTP id EA63C140023E for ; Fri, 12 Dec 2025 16:55:27 -0500 (EST) Received: from phl-imap-01 ([10.202.2.91]) by phl-compute-04.internal (MEProxy); Fri, 12 Dec 2025 16:55:27 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= garfieldtech.com; h=cc:content-transfer-encoding:content-type :content-type:date:date:from:from:in-reply-to:in-reply-to :message-id:mime-version:references:reply-to:subject:subject:to :to; s=fm1; t=1765576527; x=1765662927; bh=X/HVFb50es0ZTuwqEPmx4 b6SzVCx3VQfJVlzCLs/M/4=; b=YM5v80+PUkCUW87NOSQMbgcdIm12CYhRjy34p 3cYfxUhL65+50lukvEXyi1XYV+8vv7DNUYIkHswxdqN7bKR2Xd3oqHp42SGtNIVv SpN4mE27Qso3TqZ9Ch6XfWzJyVYtRKoRn5gPraFiL5IoJvAahwWwcQ/JKHMOgBkX GeYlQN0onU3kOho/PPnHN1cX2ytEv3wwR39h2BIMENxBQRG302GaGg9nhB3fLxfK qpFm7s6NiXiPbRFeEM5c2jZR+wH9Dg8Y8nBtvuLZ3mFy/CTGbVT78f1Zcsv1owiy /qpJEKBKXeZr0tmOlqbERk0e1cd2/3OePh6yRoNsdtcfiEE5w== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm1; t=1765576527; x=1765662927; bh=X /HVFb50es0ZTuwqEPmx4b6SzVCx3VQfJVlzCLs/M/4=; b=SHP++nItRQ+qzkwvS 9HsJQjkOFEmixsNPUsYsin/ECYhCkfGZF4eCCEaf1h6+mI6quB34PuyqLkdYOr+E decL1SaY5tf/9NrvDeWRbTsiqQiU1r/oEcgWp3BNPv+OuolYsxBLReEBfdLl5H3j qMjrUSgzHtrl8N031GY1VkUAqFfdeHtzb5Sps7BrQ3M2POuf12GppbQ553oGAmiZ ag6R/MaREOysj/l12vEIV7XLWwAQRl2AAPt0C3iFGSX4VH7NRHWLUI5XNMU78Me+ xFaU2Y9tn0XVikTpU71OZvwNBwzqdkBXOuwdizivBT4sJsJwgvjo9CLOnKo4L391 DbKpg== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefgedrtddtgddvleefvdcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpuffrtefokffrpgfnqfghnecuuegr ihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjug hrpefoggffhffvkfgjfhfutgfgsehtqhertdertdejnecuhfhrohhmpedfnfgrrhhrhicu ifgrrhhfihgvlhgufdcuoehlrghrrhihsehgrghrfhhivghlughtvggthhdrtghomheqne cuggftrfgrthhtvghrnhepiefhfeevkeehhfejgefhheejkeefhfejkedugeejtdejfedt ieffvdeifeevfeeinecuffhomhgrihhnpegvgihtvghrnhgrlhhsrdhiohenucevlhhush htvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehlrghrrhihsehgrghr fhhivghlughtvggthhdrtghomhdpnhgspghrtghpthhtohepuddpmhhouggvpehsmhhtph houhhtpdhrtghpthhtohepihhnthgvrhhnrghlsheslhhishhtshdrphhhphdrnhgvth X-ME-Proxy: Feedback-ID: i8414410d:Fastmail Received: by mailuser.phl.internal (Postfix, from userid 501) id B408A18C004E; Fri, 12 Dec 2025 16:55:27 -0500 (EST) X-Mailer: MessagingEngine.com Webmail Interface Precedence: list list-help: list-unsubscribe: list-post: List-Id: x-ms-reactions: disallow MIME-Version: 1.0 X-ThreadId: ACP5EZsL2tGZ Date: Fri, 12 Dec 2025 15:54:47 -0600 To: "php internals" Message-ID: In-Reply-To: <21f12343-b212-456f-93b3-079810d3d76d@rwec.co.uk> References: <1F3473C7-5D83-48D3-964E-A63D6F44D21E@rwec.co.uk> <4998b4c6-0474-4f0c-b63a-9909b8acfa96@bastelstu.be> <018421f64342a0d960589b4c8eea5cc5@bastelstu.be> <84b9dc16-3eb3-4283-b015-3af29fc0e55d@rwec.co.uk> <590fa655-d170-43f2-984c-d0a5ff6c30e4@bastelstu.be> <7c623161-cde3-4fc0-944c-ddfc2785c845@rwec.co.uk> <21f12343-b212-456f-93b3-079810d3d76d@rwec.co.uk> Subject: Re: [PHP-DEV] [RFC][Discussion] use construct (Block Scoping) Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable From: larry@garfieldtech.com ("Larry Garfield") On Thu, Dec 11, 2025, at 4:21 PM, Rowan Tommins [IMSoP] wrote: >> Being able to declare variables with =E2=80=9Cif=E2=80=9D lifetime th= at I can also=20 >> check is a big part of the benefits of the proposed syntax and=20 >> something I'm missing in other languages. >> >> =C2=A0=C2=A0=C2=A0 if (let $user =3D $repository->find(1); $user !=3D= =3D null) { } > > I find this more readable than the proposed version: > > >> =C2=A0 =C2=A0 let ($user =3D $repository->find(1)) if ($user !=3D=3D = null) { } > > > Skimming down a piece of code, I can spot where code is being run=20 > conditionally without reading the condition itself: > > if ............ { > > With the proposed syntax, that first glance is: > > let ........... { > > On closer inspection, it's actually: > > let ..... if ..... { > > > Maybe it's also because I've dabbled in Perl, which has post-fix=20 > conditions, so a very similar line would have a very different meaning: > > my $foo=3Ddo_bar() if ($baz !=3D 0); > > is equivalent to: > > my $foo; > if ($baz !=3D 0) { $foo=3Ddo_bar(); } > > Which is also a word order we can use in English, e.g. "hang the wet=20 > clothes inside if it is raining". > > > In terms of making it less of a special case, some languages have a=C2= =A0","=20 > operator which lets you glue any two expressions together and get the=20 > right-hand result. > > In Perl, you can write this: > > ``` > my $a =3D 'outer', $b =3D 'whatever'; > if ( my $a=3D'inner', $b =3D=3D 'whatever' ) { > =C2=A0 =C2=A0 say $a; // 'inner' > } > say $a; // 'outer' > ``` > > This gives the desired scope for $a, but the if statement is still jus= t=20 > accepting a single expression. > > JavaScript has the same operator, but apparently doesn't allow "let" i= n=20 > an expression, so you can write: > > if ( a=3D"inner", b=3D=3D"whatever" ) { } > > but can't use it to declare a local version of "a". > > > I haven't thought through exactly how to apply that to PHP, but it mig= ht=20 > give us an option for "both and": a concise and reusable syntax for th= e=20 > if use case, and a separate syntax for cases like the closure example = I=20 > gave earlier: https://externals.io/message/129059#129075 The more I think on this, the more I think that the auto-unsetting behav= ior of a `let` would be useful *only* in combination with some other exi= sting block, not as its own block. if (let $x=3Dstuff(); $x < 1) { ... } for (let $i =3D 0; $i < 10; $i++) { ... } foreach (let $arr as $k =3D>v) { ... } (applies to both $k and $v) And so on. (I'm not sure if it makes sense on a while? Possibly.) Exa= ct syntax above is just spitballing. But that would allow for the mask/unset logic for variables that have sp= ecial meaning in existing block constructs, which is generally what you'= d be interested in. I don't think there's a huge use case for unsetting= arbitrary variables in arbitrary places. It would also be cleaner than= the current pattern of if ($x =3D stuff() && $x < 1) {}, which always f= elt clunky and "leaks" $x. If you need some thing more arbitrary and custom than cleaning up an exi= sting block construct, then the additional setup of a Context Manager is= fully justified, and more robust. --Larry Garfield