Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:129589 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 6A90F1A00BC for ; Thu, 11 Dec 2025 22:21:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1765491708; bh=FKbZb9KlWHQMVb8G3NsOCyI0yfVl4j5x5DLENToWu9k=; h=Date:Subject:To:References:From:In-Reply-To:From; b=SfpEyiC8u5qqdOvBafkbMtvB1sFU0q+hLP0MJ8EshJkczsrFutfy8SwrJphv06rHo zdzgQ7HlDLDRU+1iLbBI/MudVTFErfF/Rf9+P55p2aQnx8+biQFeXBas2wSH8j147j J2DoL9QCBbSK466R6Eu9I6YLM0kE8wgOnHRPVC9tw0aZ5nLkC4UznnlO7BUHYs6032 9XLUie6V2gIEm2tXjUPvaBZakURPCBw1ufB43k7bBfgD0CDtrxzHOQsOtIXLVIydDc 4Sh5lsgZRmzXi5tneFpZq2vvAbq2JWSrBRDlGkIJ7Afjls8rMPSS+SWNh/QOItYfDe 4DLygS9qaYo5Q== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 2A91D180508 for ; Thu, 11 Dec 2025 22:21:47 +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_PASS autolearn=no autolearn_force=no version=4.0.1 X-Spam-Virus: No X-Envelope-From: Received: from fhigh-a3-smtp.messagingengine.com (fhigh-a3-smtp.messagingengine.com [103.168.172.154]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Thu, 11 Dec 2025 22:21:46 +0000 (UTC) Received: from phl-compute-03.internal (phl-compute-03.internal [10.202.2.43]) by mailfhigh.phl.internal (Postfix) with ESMTP id 9A76C140007C for ; Thu, 11 Dec 2025 17:21:41 -0500 (EST) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-03.internal (MEProxy); Thu, 11 Dec 2025 17:21:41 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rwec.co.uk; 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=1765491701; x=1765578101; bh=v4nxdlQyoZu/cdbHYXiQVwHi7QVkuMZk8qoRWMJy7h8=; b= T2R79UNg1fxJ13MQ396XGw5e3JzwDy40Ew3FYKCYut8akZLZ5NTjsgeDGvF0/8p8 ukiLMYDHuWITfZZBfDeC8mSR63yxgIPDUUpLzbBAsD07RF90FEo0o4KjxFg7n8h1 HhA6sJiCUmj2iiLMSEfgwKr1+i6azuSvxjPVVy+drinyLAjlt8PhTQNWl2VULkVE BgQCAUPAN4mjn/ZIYvGH48RSdCvLy+XDzCR2Ff7A17HtbVXlwkM7sXg2aFeE339u cTDWIMs2HgWIg3g+3L6hptRzQjjLRu8/G+wz07GCAq6R2ZTRchid1RyvnYvYa79F 1wmk6zEx4eMYFG6B9V7SxQ== 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=1765491701; x=1765578101; bh=v 4nxdlQyoZu/cdbHYXiQVwHi7QVkuMZk8qoRWMJy7h8=; b=hwKCPlD17J9lB0iiN uSJmB8oF64lcNVu1rRZamvpzOBy44DpnnxY5R3SqVBvlBnSA9heUy+DugHm0vcyE XHoc5loB6p+l7MlEFOLEsxCd55SPOpCBiUBTm6BypLdhgbUtFGM2yRqK7vP0FbLv 4GwEkhAI4+QQ0bXoe9z4NPzqF/pFKi8LTM/4tZN8ydjLoG9EYIYrRsvzl4NXFQer 1rX399immfG8G5aHYXnYayDrxge4djCYOjTImZvNFlWm/ea+ox/+DF8pZgoLEre/ 2cO51MVGzTtHlm5uZjjPd6KgFc95svke6TqhvjB8v/7thgAPwJSw2LvMjgP8MCkF hczmA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefgedrtddtgddvieehtdcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpuffrtefokffrpgfnqfghnecuuegr ihhlohhuthemuceftddtnecunecujfgurhepkfffgggfuffvfhfhjggtgfesthekredttd dvjeenucfhrhhomhepfdftohifrghnucfvohhmmhhinhhsucglkffoufhorfgnfdcuoehi mhhsohhprdhphhhpsehrfigvtgdrtghordhukheqnecuggftrfgrthhtvghrnhepgedtvd eivdekheehleduueffkeekuedtueduheelgeeiteffhfeuuedtkedvueevnecuffhomhgr ihhnpegvgihtvghrnhgrlhhsrdhiohenucevlhhushhtvghrufhiiigvpedtnecurfgrrh grmhepmhgrihhlfhhrohhmpehimhhsohhprdhphhhpsehrfigvtgdrtghordhukhdpnhgs pghrtghpthhtohepuddpmhhouggvpehsmhhtphhouhhtpdhrtghpthhtohepihhnthgvrh hnrghlsheslhhishhtshdrphhhphdrnhgvth X-ME-Proxy: Feedback-ID: id5114917:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA for ; Thu, 11 Dec 2025 17:21:41 -0500 (EST) Message-ID: <21f12343-b212-456f-93b3-079810d3d76d@rwec.co.uk> Date: Thu, 11 Dec 2025 22:21:40 +0000 Precedence: list list-help: list-unsubscribe: list-post: List-Id: x-ms-reactions: disallow MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PHP-DEV] [RFC][Discussion] use construct (Block Scoping) To: internals@lists.php.net 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> Content-Language: en-GB In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit From: imsop.php@rwec.co.uk ("Rowan Tommins [IMSoP]") On 10/12/2025 16:23, Tim Düsterhus wrote: > > That sentence you quoted was specifically in the context of the > initial paragraph of that section, contrasting PHP - where block > scoping is expected to be used comparatively sparingly - against > languages where variable declarations are a more “bread and butter” > part of the development process, because formally / explicitly > declaring variables is a necessity for one reason or another. I don't think that changes anything I said in my previous reply: as soon as you declare a variable half-way through a block, there is an ambiguity about its range of visibility. Having more variable declarations makes that *more* likely to come up, not *less*, so I'm not sure why you think it "avoids" the problem. There's also an assumption that if PHP added block scoping, it would only rarely be used. We have no way to know, but I'm not sure that's true. I can easily imagine code styles adding a rule that all local variables be declared at an appropriate level. I can also imagine new users coming from other languages - particularly JS - adding "let" out of habit, even if seasoned PHP coders wouldn't. > I feel that the C99 requirements and syntax would still have more > ambiguity compared to the proposed `let()` syntax in cases like this: > >     { >         let $foo = bar($baz); // What is $baz referring to? > Particularly if it is a by-reference out parameter. > >         let $baz = 1; >     } Probably the simplest solution is to re-use our existing definition of "constant expression". In fact, we already have variable declarations using that rule: function foo() {     static $a = 1; // OK     static $b = $a; // Fatal error: Constant expression contains invalid operations } > As an example, is a goto jump label a statement? > >     { >         let $foo = 1; >  label: >         let $bar = $foo++; >         goto label; >     } PHP already limits where "goto" can jump to; I don't know how that's implemented, but I don't think we need to get into philosophical definitions to say "you can't jump into the middle of a declaration list". Or, we could just bite the bullet and answer the "which way does it resolve" question, as loads of other languages have already done. > Being able to declare variables with “if” lifetime that I can also > check is a big part of the benefits of the proposed syntax and > something I'm missing in other languages. > >     if (let $user = $repository->find(1); $user !== null) { } I find this more readable than the proposed version: >     let ($user = $repository->find(1)) if ($user !== null) { } Skimming down a piece of code, I can spot where code is being run 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 conditions, so a very similar line would have a very different meaning: my $foo=do_bar() if ($baz != 0); is equivalent to: my $foo; if ($baz != 0) { $foo=do_bar(); } Which is also a word order we can use in English, e.g. "hang the wet clothes inside if it is raining". In terms of making it less of a special case, some languages have a "," operator which lets you glue any two expressions together and get the right-hand result. In Perl, you can write this: ``` my $a = 'outer', $b = 'whatever'; if ( my $a='inner', $b == 'whatever' ) {     say $a; // 'inner' } say $a; // 'outer' ``` This gives the desired scope for $a, but the if statement is still just accepting a single expression. JavaScript has the same operator, but apparently doesn't allow "let" in an expression, so you can write: if ( a="inner", b=="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 might give us an option for "both and": a concise and reusable syntax for the if use case, and a separate syntax for cases like the closure example I gave earlier: https://externals.io/message/129059#129075 -- Rowan Tommins [IMSoP]