Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:126826 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 qa.php.net (Postfix) with ESMTPS id 9C3731A00BC for ; Tue, 18 Mar 2025 15:14:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1742310700; bh=z5hBvGpl6eIiRn5KP6WE8NC1nvdQIf0IwinxYWKc7Nk=; h=Date:To:From:Subject:From; b=YH1CvW2zWrCc1RPrm55wvN2XC8YqcJ6wARXT+Uy/xdT0Pjt/9/YmR+abGFgjgHMSa ThU2rk6MFZjVQ619oYNz57O1VFa4r80ZbqjulDtam84FLghrwdnIzmGt7ehOHaI5K4 1V4CeM7Ve04Ai0LrBd9q5Cmigexr/D51pBzAdMk7U0g/7TGtBoBu98WmwyHr1H0iie D4aCpHJl9amn7bQinJMDVOk2GiLjDIw0nNR0fPfDUDh7sKxPt14cH67idqdVfOc5U+ znvQiRQ4VfPGkGVig33KmVh443BhHU/7GME9b+RyXY2YOyq6YWdzY14Gj6iW8JJRqg 9NPUXoIiLGCgA== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id AEAE518006B for ; Tue, 18 Mar 2025 15:11:39 +0000 (UTC) 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.5 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DMARC_PASS,FORGED_MUA_MOZILLA,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_PASS,SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: <010f0195a9d1bdbe-14e38a2a-405a-4516-8b3d-8d3065958bda-000000@mailer.miqrogroove.com> Received: from e226-3.smtp-out.us-east-2.amazonses.com (e226-3.smtp-out.us-east-2.amazonses.com [23.251.226.3]) (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, 18 Mar 2025 15:11:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/simple; s=qxgm6rwwab7twixglbexlc5btumk6x2k; d=miqrogroove.com; t=1742310850; h=Content-Type:Message-ID:Date:MIME-Version:To:From:Subject; bh=z5hBvGpl6eIiRn5KP6WE8NC1nvdQIf0IwinxYWKc7Nk=; b=tG5ahfBx2pb9SrfReW4iNW3l4EJz9Neuc/z6nlkuAxAeDSI78/ymuLf0xNiEsJab KUSmyI9K3MMSMkEfd9y+zE34NYhypNiDZcxVql9njOZopmkWkr+5uxmD3zDR0h4Y8Nx 2MjH9ox7svEJnd7ZDuaF4sXIH1+xBmoMherhjhUIZ7ErTv2ohgSverBPyHHDmyinWtz MaCvMSTdQ3x5jjxxWBfF/RAXStDVVBaw4c8EKNrOIZwEa/9IEs1NI6dqjVEwj8Bg7Jc T0MTH67/2nJnhNfDwrKZvo4wFwKPm1xna39wyrCUpAdnGp2Z116I9LC9qVBzsQHyWoq gJ4Fcs+4gQ== DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/simple; s=xplzuhjr4seloozmmorg6obznvt7ijlt; d=amazonses.com; t=1742310850; h=Content-Type:Message-ID:Date:MIME-Version:To:From:Subject:Feedback-ID; bh=z5hBvGpl6eIiRn5KP6WE8NC1nvdQIf0IwinxYWKc7Nk=; b=YEAe7sTk7bu8C4Z4UYKG6vhJCJ1jiHTV5FQMeKR47MH7UG9gyDgRPS34jWxRy+6G RpWlyc1WKdLz4oAz/euARK5Nwms58n9DyFX8Wr8AZGpKSydAXJww7Vx1TdODh0FUOvq riwuoAIZ4+KiraPnknQK9IoyXes/gtLJNj0XOSYA= Authentication-Results: solid; auth=pass (plain) Content-Type: multipart/alternative; boundary="------------fFvFb7bQxYOzdmR0aN0Y0tVL" Message-ID: <010f0195a9d1bdbe-14e38a2a-405a-4516-8b3d-8d3065958bda-000000@us-east-2.amazonses.com> Date: Tue, 18 Mar 2025 15:14:09 +0000 Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 User-Agent: Mozilla Thunderbird To: internals@lists.php.net Content-Language: en-US Subject: [PHP-DEV] Feature: Null Coalescing Variable Handling Construct DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=miqrogroove.com; s=jan2022; h=subject:from:to; bh=z5hBvGpl6eIiRn5KP6WE8NC1nvdQIf0IwinxYWKc7Nk=; b=GpgnZ01WLxTRhMvre+9n7jeunYNmzFQ03nfwUfGsVfne66k/eZdYhEFhnV6qKeQwaAmB13s3ni c6PvJ0LFZhJRcyrXc5St6XJVEoiXJ3zeIRhFkg6grcHBtlGxnVw9GbDGGgWdzWXSJNMENnOJpiFT 1WnEmgEGIBi0UI1m2z6INkm6cQNn6fZ9mjWLN5nWsyrpthB0IfbAYHwqB5WSl1bHE59Zco/M4bgZ M39EPg2a5o0rhOxnLmrwmY/o7wWXlvD+GMtCxjLCj+ViR1nwrJUexv9ji3gMcgUoka/YlNfUOehV ZS4pOt+jD01KnwoYaBA5lrpnLCEh6vvDavzahKjw== Feedback-ID: ::1.us-east-2.QbD2JL3ZnDgkTsZd92lLMWNtgKUB27BqpIzxIe67R3Y=:AmazonSES X-SES-Outgoing: 2025.03.18-23.251.226.3 From: php@miqrogroove.com (Robert Chapin) This is a multi-part message in MIME format. --------------fFvFb7bQxYOzdmR0aN0Y0tVL Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Following a brief discussion on Github today about adding operators, I decided a simple-looking construct is needed and is arguably a better solution for null coalescing comparison. A common challenge: Need to test the value of a variable whose existence is unknown. Existing solutions: |$input = $_POST['input'] ?? 'none'; if ($input === 'yes') echo 'success'; $_POST['input'] ??= 'none'; if ($_POST['input'] === 'yes') echo 'success'; | Needed solution: What we don't have is a Variable Handling Function that will concisely resolve any uninitialized variable. // Hypothetical |if (nullc($_POST['input']) === 'yes') echo 'success'; | The proposed construct nullc(mixed $var): mixed, will return the original value, or null if the variable is not declared.  This is consistent with the construct isset which returns false when a variable is assigned to null.  In this way, the two constructs isset and nullc will complement each other more concisely than something like the construct empty. The other challenge: Null coalescing operator precedence causes unexpected behavior. $test = 'off'; if ($test ?? '' === 'on') {     // The line above is equivalent to if ($test ?? false) and will evaluate as true.     echo 'yes'; } if ('on' === $what ?? '') {     // The line above is equivalent to if (('on' === $what) ?? '') and will throw a warning } This requires an extra set of parentheses to be workable. $test = 'off'; if (($test ?? '') === 'on') {     // The line above will evaluate as false. } These nuances were missed in the former Unary Coalesce RFC. Adding or not adding an operator doesn't resolve the cumbersome precedence already in place for ?? usage.  I was reminded of the need for a function-style construct while contemplating the differences between PHP's ?? operator and Microsoft's function Nz.  What we can do with an expression like nullc($test) === 'on' should be compatible with other existing operators and is more elegant than operators-in-parentheses or any multi-line statements.  This would also resolve the problem of not being able to do the same in a user function. Further reading: https://wiki.php.net/rfc/unary_null_coalescing_operator ------------- Robert Chapin --------------fFvFb7bQxYOzdmR0aN0Y0tVL Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit

Following a brief discussion on Github today about adding operators, I decided a simple-looking construct is needed and is arguably a better solution for null coalescing comparison.

A common challenge: Need to test the value of a variable whose existence is unknown.

Existing solutions:

$input = $_POST['input'] ?? 'none';
if ($input === 'yes') echo 'success';

$_POST['input'] ??= 'none';
if ($_POST['input'] === 'yes') echo 'success';


Needed solution: What we don't have is a Variable Handling Function that will concisely resolve any uninitialized variable.

// Hypothetical
if (nullc($_POST['input']) === 'yes') echo 'success';

The proposed construct nullc(mixed $var): mixed, will return the original value, or null if the variable is not declared.  This is consistent with the construct isset which returns false when a variable is assigned to null.  In this way, the two constructs isset and nullc will complement each other more concisely than something like the construct empty.


The other challenge: Null coalescing operator precedence causes unexpected behavior.

$test = 'off';

if ($test ?? '' === 'on') {
    // The line above is equivalent to if ($test ?? false) and will evaluate as true.
    echo 'yes';
}

if ('on' === $what ?? '') {
    // The line above is equivalent to if (('on' === $what) ?? '') and will throw a warning
}

This requires an extra set of parentheses to be workable.

$test = 'off';

if (($test ?? '') === 'on') {
    // The line above will evaluate as false.
}

These nuances were missed in the former Unary Coalesce RFC.  Adding or not adding an operator doesn't resolve the cumbersome precedence already in place for ?? usage.  I was reminded of the need for a function-style construct while contemplating the differences between PHP's ?? operator and Microsoft's function Nz.  What we can do with an expression like nullc($test) === 'on' should be compatible with other existing operators and is more elegant than operators-in-parentheses or any multi-line statements.  This would also resolve the problem of not being able to do the same in a user function.

Further reading:

https://wiki.php.net/rfc/unary_null_coalescing_operator

-------------
Robert Chapin
--------------fFvFb7bQxYOzdmR0aN0Y0tVL--