Re: Feedback for nullc Idea

From: Date: Tue, 25 Mar 2025 16:42:45 +0000
Subject: Re: Feedback for nullc Idea
References: 1 2 3 4 5  Groups: php.internals 
Request: Send a blank email to [email protected] to get a copy of this message
On 3/25/2025 4:45 AM, Rowan Tommins [IMSoP] wrote:
My point is that because null is not going to be coerced by the language to either 'on' nor 'off', there's an implied default depending how you write the expression. That kind of user mistake is hard for me to wrap my mind around.  I don't expect a missing variable to be identical to a string literal.  So a unary coalesce seems perfectly natural to me.
The implied default in the first is 'off', but in the second it's 'on'. I thought the implied default was null.  $input === 'on' is only identical for 'on'.  $input !== 'off' is always not identical unless 'off'.
A different example could be if (coalesce($_POST['tick']) > 10) return;
In this case, the implied default is 0.
I thought the implied default was null.  For me, potential confusion would arise in situations where a database query might return a null value and I have to choose between is_null() or coalesce() or ($a ?? "").  That's the situation where returning null doesn't accomplish anything and the single-var coalesce becomes the wrong choice.
I don't know if I'd go as far as banning a single-argument coalesce, but I would definitely discourage its use.
The feedback and the thoughtful perspective on user mistakes are helpful.  Thank you again. ------------- Robert Chapin

Thread (10 messages)

« previous php.internals (#126934) next »