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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: folarin at engineer dot com
New email:
PHP Version: OS:

 

 [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

Add a Patch

Pull Requests

Add a Pull Request

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: Fri Apr 19 20:01:29 2024 UTC