Skip to content

Specify Null Coalesce Operator isset Relation#5527

Open
Krause-a wants to merge 3 commits intophp:masterfrom
Krause-a:master
Open

Specify Null Coalesce Operator isset Relation#5527
Krause-a wants to merge 3 commits intophp:masterfrom
Krause-a:master

Conversation

@Krause-a
Copy link
Copy Markdown
Contributor

@Krause-a Krause-a commented May 1, 2026

This PR addresses issue #5526

Make the isset relationship with ?? explicit to reduce potential confusion.

Make the connection to `isset` explicit and distinct from the null check to match PHP behavior. Remove redundant warning suppression information.
Comment thread language/operators/comparison.xml Outdated
Comment thread language/operators/comparison.xml
Comment thread language/operators/comparison.xml Outdated
Comment thread language/operators/comparison.xml Outdated
Comment on lines +476 to +478
<replaceable>expr2</replaceable> if <replaceable>expr1</replaceable> is not set,
as determined by <function>isset</function>, or is &null;.
Otherwise, it evaluates to <replaceable>expr1</replaceable>.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be noted that isset() returns false is the value is defined but set to null.

While I think the wording here could be improved to make the behavior clearer, I think the suggested change here potentially causes confusion about the behavior of isset.

It might be better to omit mention of isset here entirely and just say:

if expr1 is not defined or is &null;.```

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the feedback!

The driving factor for the inclusion of isset() is that both the isset check and the null check appear to be performed separately. I understand that this is redundant for array checks and variable checks. However, the seemingly redundant null check matters for object properties when using the __isset magic method.

class MyClass { # Minimal Example Class
    public function __isset($name) {
        return true;
    }
    public function __get($name) {
        return null;
    }
}
$my_obj = new MyClass;
# Ex.1
var_dump($my_obj->fake ?? 'rhs'); # rhs
# Ex.2
var_dump(isset($my_obj->fake) ? $my_obj->fake : 'rhs'); # null
# Ex.3
var_dump(isset($my_obj->fake) && $my_obj->fake !== null ? $my_obj->fake : 'rhs'); # rhs

You can try the different combinations with __isset returning true and false and __get returning null or 123.

if expr1 is not defined or is &null;

My worry is that this will be confused with only an isset check, since, in normal circumstances, isset will do the null check, making the second null check redundant. The documentation on isset starts with "Determine if a variable is declared and is different than null." The isset documentation sounds very similar. It could be believed that it is, in fact, exactly the same. The PHP 7.0 new features announcement includes the "... if it exists and is not null..." wording and references ?? as syntax sugar for an isset ternary. Do you have an alternative wording that would still have the isset and null check as distinct steps in the ?? operator?

Copy link
Copy Markdown
Contributor

@AllenJB AllenJB May 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So it turns out this is a known oddity of isset() and the magic methods.

My personal feeling is that if this behavior is documented, it belongs under either (or both) the isset() or magic methods documentation.

The only change I would suggest to the null coalesce documentation, if any, is to say:

if expr1 is not set or is null

Related:

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, wow! Thank you for the links! In light of this information, I agree with a less explicit reference to isset. Let me know what you think :)

Krause-a added 2 commits May 5, 2026 11:35
Use simpara as per guidelines
Reduce `isset` tie in for possible future changes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants