php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #79894 ternary condition perturbates if condition
Submitted: 2020-07-24 17:42 UTC Modified: 2020-07-24 17:44 UTC
From: fabien dot aurejac at gmail dot com Assigned:
Status: Closed Package: *General Issues
PHP Version: 7.2.32 OS: debian 10.4
Private report: No CVE-ID: None
 [2020-07-24 17:42 UTC] fabien dot aurejac at gmail dot com
Description:
------------
When I try this script it should at my point of view return 0 instead of 1 ?
am I mistaken ?

Test script:
---------------
<?php
if (false && true?"1":"2") {
	echo 1;
} else {
	echo 0;
}
?>

Expected result:
----------------
0

Actual result:
--------------
1

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-07-24 17:44 UTC] fabien dot aurejac at gmail dot com
-Status: Open +Status: Closed
 [2020-07-24 17:44 UTC] fabien dot aurejac at gmail dot com
Ok, seems
 [2020-07-24 17:47 UTC] fabien dot aurejac at gmail dot com
seems to be an error on my side... I omit the priority of operators
 [2020-12-17 15:46 UTC] andrew dot engdahl at gmail dot com
The example given returns the correct result. 
The ternary operator is would always return a string which is always truthy. 
Even the integers 1 or 2 would evaluate as true; however, 0 would be false.

Only the following would provide the result of 0:

if (false && true ? 1 : 0) {
    echo 1;
} else {
    echo 0;
}
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu May 16 22:01:31 2024 UTC