Bug #70021 [Opn]: incorrect associativity of ternary operator
Edit report at https://bugs.php.net/bug.php?id=70021&edit=1
ID: 70021
User updated by: mega at ioexception dot at
Reported by: mega at ioexception dot at
Summary: incorrect associativity of ternary operator
Status: Open
Type: Bug
Package: Scripting Engine problem
PHP Version: 7.0Git-2015-07-08 (Git)
Block user comment: N
Private report: N
New Comment:
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'.
Previous Comments:
------------------------------------------------------------------------
[2015-07-08 12:18:28] 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
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=70021&edit=1
Thread (5 messages)