php.net |  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
Votes:6
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
Description:
------------
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:
----------------
Toast

Actual result:
--------------
Swiss Cheese

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

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] cmb@php.net
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] <http://cmsimpleforum.com/viewtopic.php?f=29&t=8821>
[2] <https://github.com/php/php-src/releases/tag/php-7.0.0beta1>
 [2015-07-08 13:45 UTC] cmb@php.net
Oops, wrong link to [1]. Should have been
<http://markmail.org/thread/5bjy2xnu2vrgxxmy>.
 [2018-03-26 13:30 UTC] cmb@php.net
-Status: Open +Status: Suspended -Type: Bug +Type: Feature/Change Request
 [2018-03-26 13:30 UTC] cmb@php.net
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] <https://wiki.php.net/rfc/howto>
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Sun Jul 21 08:01:25 2019 UTC