Re: Null Coalesce Handling Revised
| From: | Tim Düsterhus | Date: | Mon, 23 Nov 2026 20:57:08 +0000 |
| Subject: | Re: Null Coalesce Handling Revised | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-133210@lists.php.net to get a copy of this message | ||
Hi
On 2026-11-23 17:54, Robert Chapin wrote:
Order of operation problems are solved by calling this hypothetical function instead of using the ?? operator. In other words, if $b is unset and someone attempts if ($a === $b ?? []) we have a missing set of parentheses and a runtime warning. However, `if ($a === array_coalesce($b))` becomes an intuitive and viable approach to testing the value of $b.
Foreseeable questions:
Would these parameters be implicitly nullable?
Is there a significant need for this feature?
Staying with your specific example of if ($a === $b ?? []) and even though I'm usually a big proponent of putting stuff into the language instead of third party tools: I believe it is the job of a linter to point out that this construct is likely not doing what you intended it to do, because bool ?? anything is always the bool.
Generally, I believe that one should almost always use explicit parentheses when mixing different operators within a single expression [1]. Parentheses are cheap, confusion is expensive.
So, no I don't think there is a significant need for this feature that warrants the additional language complexity and impact on the ecosystem (e.g. IDEs or static analysis tools, who are hopefully already capable of pointing out the missing parens).
Best regards
Tim Düsterhus
[1] Perhaps with the exception of comparisons with either && or || (but not mixing them, I've specifically written https://github.com/PHPCSStandards/PHP_CodeSniffer/blob/4.x/src/Standards/Generic/Sniffs/CodeAnalysis/RequireExplicitBooleanOperatorPrecedenceSniff.php against that).