|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #70021 incorrect associativity of ternary operator
Submitted: 2015-07-08 12:18 UTC Modified: 2018-03-26 13:30 UTC
Avg. Score:4.7 ± 0.7
Reproduced:4 of 4 (100.0%)
Same Version:2 (50.0%)
Same OS:3 (75.0%)
From: mega at ioexception dot at Assigned:
Status: Suspended Package: Scripting Engine problem
PHP Version: 7.0Git-2015-07-08 (Git) OS:
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2015-07-08 12:18 UTC] mega at ioexception dot at
zend_language_parser.y on line 81 gives the '?:' operator left associativity when 
it should be right as in virtually every other language that has a ternary 
operator. [see also bug 61915]

Due to the massive amount of changes in PHP 7, some of which break backwards compatibility, it would be a great idea to fix this long standing issue. Although it has been pointed out before that this behavior is documented it's a non-sane and unconventional result. In other languages that implement the ternary operator the result of the test script is 'Swiss Cheese'. This result is expected and conventional.

This issue has been pointed out multiple times before (61915, 25356).

Test script:
$arg = "3";
$food = (  ($arg == '1') ? 'Banana' :
           ($arg == '2') ? 'Apple' :
           ($arg == '3') ? 'Toast' :
           ($arg == '4') ? 'Cantalope' :
           ($arg == '5') ? 'Swiss Cheese' : 'Fig Newton Cookie'
echo $food;

Expected result:

Actual result:
Swiss Cheese


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2015-07-08 12:37 UTC] mega at ioexception dot at
A typo has been made in the description. The result in other languages that implement the ternary operator the result of the test script is 'Toast' not 'Swiss Cheese'.
 [2015-07-08 13:44 UTC]
There has been quite some discussion about this isse last
December[1], but to my knowledge nothing came out of it. Anyhow,
PHP 7.0.0beta1 has already been tagged[2] which is commonly
regarded as feature freeze.

[1] <>
[2] <>
 [2015-07-08 13:45 UTC]
Oops, wrong link to [1]. Should have been
 [2018-03-26 13:30 UTC]
-Status: Open +Status: Suspended -Type: Bug +Type: Feature/Change Request
 [2018-03-26 13:30 UTC]
Due to the controversial nature of this change, it requires the
RFC process[1].  Anybody is welcome to start it.  For the time
being, I'm suspending this ticket.

[1] <>
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Mar 02 21:01:29 2024 UTC