php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #51546 If shorthand statement
Submitted: 2010-04-13 04:23 UTC Modified: 2010-04-13 22:32 UTC
From: michaelozeryansky at hotmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.2.13 OS: Linux
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: michaelozeryansky at hotmail dot com
New email:
PHP Version: OS:

 

 [2010-04-13 04:23 UTC] michaelozeryansky at hotmail dot com
Description:
------------
When I use nested shorthand if statements the result doesn't seem to follow what I expected. I am not sure if this is a bug, but the results are different in other languages.

Test script:
---------------
<?php

var_dump ((true) ?"a":(true) ?"b":"c") ; // b
var_dump ((true) ?"a":(false)?"b":"c") ; // b
var_dump ((false)?"a":(true) ?"b":"c") ; // b
var_dump ((false)?"a":(false)?"b":"c") ; // c

?>




I wrote a c++ script to see what the results should be in another language
cout << ((true) ?"a":(true) ?"b":"c") << endl; // result = a
cout << ((true) ?"a":(false)?"b":"c") << endl; // result = a
cout << ((false)?"a":(true) ?"b":"c") << endl; // result = b
cout << ((false)?"a":(false)?"b":"c") << endl; // result = c

Expected result:
----------------
The PHP script should print out:
a
a
b
c

Actual result:
--------------
In the comments behind each line

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-04-13 05:52 UTC] aharvey@php.net
-Status: Open +Status: Bogus
 [2010-04-13 05:52 UTC] aharvey@php.net
As the note at the end of the manual page at http://au2.php.net/ternary hints at, the associativity of the ternary operator is not particularly obvious. We can't change this now due to potential backward compatibility issues.
 [2010-04-13 22:32 UTC] michaelozeryansky at hotmail dot com
Thank you, I was forgot to mention if i used parenthesis then it works fine, but I still thought there was an issue.

Now I know, and thank you.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Aug 13 00:00:03 2025 UTC