Bug #78017 [Com]: Null coalesce operator in if paranthesis causes unexpected behaviour

[email protected] ("dorndorf at featdd dot de") Wed, 15 May 2019 14:58:43 +0000
Newsgroups php.standards
Message-ID <[email protected]>
Edit report at https://bugs.php.net/bug.php?id=78017&edit=1

 ID:                 78017
 Comment by:         dorndorf at featdd dot de
 Reported by:        dorndorf at featdd dot de
 Summary:            Null coalesce operator in if paranthesis causes
                     unexpected behaviour
 Status:             Not a bug
 Type:               Bug
 Package:            PHP Language Specification
 Operating System:   macOS 10.14.4
 PHP Version:        7.2.18
 Block user comment: N
 Private report:     N

 New Comment:

oh my shame, I had a more complex example in my project where this case can easily be misunderstood.

Thanks for the quick response, learned something interesting new today :-)


Previous Comments:
------------------------------------------------------------------------
[2019-05-15 13:37:17] [email protected]

?? has lower precedence than &&, so this is true ?? (false && 0 > 0). See https://www.php.net/manual/en/language.operators.precedence.php for operator precedence in PHP.

------------------------------------------------------------------------
[2019-05-15 13:26:07] dorndorf at featdd dot de

Description:
------------
Using the null coalesce operator inside an if paranthesis using and resulting in false will match the condition anyway when the returned value is true.

Test script:
---------------
if (true ?? false && 0 > 0) {
    echo 'Should not be true';
}



------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=78017&edit=1