php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #79970 Null coalescing operator needs to have a higher precedence value
Submitted: 2020-08-13 12:43 UTC Modified: 2020-08-13 13:35 UTC
From: folarin at engineer dot com Assigned:
Status: Not a bug Package: *Programming Data Structures
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
 [2020-08-13 12:43 UTC] folarin at engineer dot com
Description:
------------
Look at the following example

$var1 = true
$var2 = true

if($var1??false && $var2)
echo 'Passed"
else
echo 'Failed'

The above expresion always prints 'Failed' which isn't the expected behaviour.
Just like the Parenthesis, the null-coalescing operator should ALWAYS be expressed first.

This now causes the unwanted, over-expressive use of parenthesis to remedy the cause ALL THE TIME:

if(($var1??false) && ($var2??false))...

why do I have to do the above when none of 


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-08-13 12:46 UTC] cmb@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.

> why do I have to do the above when none of 

if (!empty($var1) && !empty($var2))
 [2020-08-13 12:47 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2020-08-13 12:47 UTC] cmb@php.net
.
 [2020-08-13 12:48 UTC] nikic@php.net
-Assigned To: cmb +Assigned To:
 [2020-08-13 12:48 UTC] nikic@php.net
Precedence is a trade-off. Use explicit parentheses in cases where it is not clear to you.

The current choice of precedence is for consistency with the ?: operator, and consistency across languages (e.g. matches C# operator precedence).
 [2020-08-13 13:35 UTC] folarin at engineer dot com
Fair enough
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 13:01:31 2024 UTC