Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Null Coalescing Comparison #18104

Closed
miqrogroove opened this issue Mar 18, 2025 · 8 comments
Closed

Null Coalescing Comparison #18104

miqrogroove opened this issue Mar 18, 2025 · 8 comments

Comments

@miqrogroove
Copy link

miqrogroove commented Mar 18, 2025

Description

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 an operator that will compare an uninitialized variable.

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

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.
}

Further reading, 2017 Unary Null Coalescing

A unary operator would have been too easily confused for the binary operator. The question now is whether one or more comparison operators would be useful, and whether it requires things like ?!== to be logically complete.

@iluuu1994
Copy link
Member

Hi @miqrogroove. Please propose your idea to the internals mailing list. While feature requests are allowed here, this is primarily a bug tracker and feature requests are unlikely to get anywhere.

I personally do not feel PHP needs more operators. $test ?== 'on' is barely shorter than ($test ?? '') === 'on', and as you mention with ?!==, it immediately begs the question whether we need to add all other operators for consistency. And what's definitely worse than adding another operator, is adding a dozen more.

Null coalescing operator precedence causes unexpected behavior.

There is no perfect precedence. Precedence is set in a way that is expected to cause the least surprises, and require the fewest parentheses. But ultimately, it is subjective and may diverge from your expectation. Obviously, changing it now would have BC implications.

@miqrogroove
Copy link
Author

I agree with not attempting to change operator precedence. I also think it's not about the shorter code. I would rather use two statements that make sense than one statement where I can't remember why I added parentheses. The $test ?== 'on' syntax is elegant and meaningful until contemplating a need for more operators. Maybe what's needed is a variable handling function to complement is_null. Something like nullc($test) === 'on' would be nice, since this can't be accomplished in a user function. I will give this some thought and check out the mailing list.

@iluuu1994
Copy link
Member

Unfortunately, nullc() cannot be accomplished using an internal function either, without further tweaking its argument passing semantics. At that point, we'd have an easier time implementing a new operator.

Tbh, $test?? === 'on' would have been the best alternative if this needs a solution, because it works automatically with all operators.

@miqrogroove
Copy link
Author

Is the argument passing any different from the construct isset? I see that's not technically a function but the same idea.

@iluuu1994
Copy link
Member

@miqrogroove Correct. isset() is not a function either, so whether it is an operator (i.e. $test??) or something that resembles a function nullc($test) doesn't matter in terms of the implementation. That's all I was trying to point out.

@miqrogroove
Copy link
Author

I shall strive to use correct jargon in my mailing list messaging 🤓

@miqrogroove
Copy link
Author

Sent to the list and cross posted https://www.miqrogroove.com/blog/2025/functional-null-coalesce/

Closing here.

@iluuu1994
Copy link
Member

Thanks @miqrogroove for the initiative!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants