php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #78568 Incorrect evaluation of ?? and ? containing expression
Submitted: 2019-09-19 16:30 UTC Modified: 2019-09-19 16:34 UTC
From: carlos at wfmh dot org dot pl Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 7.3.9 OS: Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: carlos at wfmh dot org dot pl
New email:
PHP Version: OS:

 

 [2019-09-19 16:30 UTC] carlos at wfmh dot org dot pl
Description:
------------
Incorrect evaluation of ?? and ? containing expression.

This code produces incorrect results:

<?php
$result = 20;
$success = false;

$result = $result ?? $success ? "TRUE" : "FALSE";
var_dump($result);

would output "TRUE" instead of expected 20.

Workaround is to put ternary into brackets:

<?php
$result = 20;
$success = false;

$result = $result ?? ($success ? "TRUE" : "FALSE");
var_dump($result);

and then the result is as expected 20.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-09-19 16:34 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2019-09-19 16:34 UTC] requinix@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

https://www.php.net/manual/en/language.operators.precedence.php
?? has higher precedence than ?:
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Tue Apr 13 05:01:24 2021 UTC