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
 [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-2024 The PHP Group
All rights reserved.
Last updated: Wed Apr 24 22:01:30 2024 UTC