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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
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

Pull Requests

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: Sat Dec 14 19:01:26 2024 UTC