Re: Feedback for nullc Idea
On 25 March 2025 05:53:30 GMT, Robert Chapin <[email protected]> wrote:
>I may have over-simplified the examples. Comparing $input === 'yes' will have the
>same result whether $input is null or 'none' or an empty string. So not implying a
>result type, just want to compare a literal or other variable to $input even when not declared.
The examples are fine, I think, but perhaps I didn't explain mine clearly enough.
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.
The following both evaluate to true for an input of 'on', and false for an input of
'off', but give different results for null:
$input === 'on'
$input !== 'off'
The implied default in the first is 'off', but in the second it's 'on'.
>A different example could be if (coalesce($_POST['tick']) > 10) return;
In this case, the implied default is 0. It's less risky, because the coercion from null to int
is straightforward. The following all imply a default of zero:
if (coalesce($_POST['tick']) >= 10) return;
if (coalesce($_POST['tick']) != 10) return;
if (coalesce($_POST['tick']) !== 10) return;
if (coalesce($_POST['tick']) > 0) return;
if (coalesce($_POST['tick']) >= 0) return;
if (coalesce($_POST['tick']) != 0) return;
But this doesn't:
if (coalesce($_POST['tick']) !== 0) return;
By specifying the default explicitly, we don't have to examine the expression carefully to see
what's implied.
I don't know if I'd go as far as banning a single-argument coalesce, but I would
definitely discourage its use.
Rowan Tommins
[IMSoP]
Thread (10 messages)