|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2012-05-02 22:16 UTC] iam4webwork at hotmail dot com
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.
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:
----------------
I expected to see 'Toast'.
Actual result:
--------------
The actual result is 'Swiss Cheese'.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Oct 25 23:00:01 2025 UTC |
Php7 would have been a good chance for fixing it, can we hope for Php8? This is not "a bad feature" programmers can work around, it just requires a useless number of parenthesis that makes code much less clear. Why not add another operator like "??" with the right associativity? (pun coincidental) $a ?? $b : $c $food = ( ($arg == '1') ?? 'Banana' : ($arg == '2') ?? 'Apple' : ($arg == '3') ?? 'Toast' : ($arg == '4') ?? 'Cantalope' : ($arg == '5') ?? 'Swiss Cheese' : 'Fig Newton Cookie' ); would yeld the (human-brain) expected result without much hassle. Of course that would bring in Super Elvis ??: