Bug #61915 [Nab]: incorrect associativity of ternary operator

From: Date: Tue, 09 Apr 2019 10:21:06 +0000
Subject: Bug #61915 [Nab]: incorrect associativity of ternary operator
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-220378@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=61915&edit=1 ID: 61915 Updated by: bishop@php.net Reported by: iam4webwork at hotmail dot com Summary: incorrect associativity of ternary operator Status: Not a bug Type: Bug Package: Scripting Engine problem Operating System: Linux,Windows PHP Version: Irrelevant Block user comment: N Private report: N New Comment: RFC "Deprecate left-associative ternary operator"[1] proposes to address this as of PHP 8. [1]: https://wiki.php.net/rfc/ternary_associativity Previous Comments: ------------------------------------------------------------------------ [2017-03-21 14:12:45] vittorio dot zamparella at gmail dot com 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 ??: ------------------------------------------------------------------------ [2016-12-16 14:37:51] tharkon at gmail dot com I am a bit annoyed by the Note a bug status. It is a bug, you just chose not to fix it. ------------------------------------------------------------------------ [2012-11-12 14:47:38] sun at unleashedmind dot com Recent major versions of PHP have introduced differences in behavior that are far worse than a correction of the ternary operator associativity would be. IMHO, correcting this should be seriously considered for PHP 5.5 (or whatever is next ;)), since the current behavior is indeed very unexpected. If PHP applications need to support older PHP major versions, then a version-agnostic ternary operator logic can be easily achieved through proper usage of parenthesis (which are required to achieve the expected ternary logic today already, right? -- which in turn, further undermines the compatibility argument?). (Also, based on my experience with other developers who struggled with the current behavior, most of them simply gave up and changed their code to if/else or switch control structures in the first place.) ------------------------------------------------------------------------ [2012-05-02 22:38:59] johannes@php.net Yes, this was an oversight when the language was defined and is documentedlike that on http://php.net/ternary (see the last note). Unfortunately we can't fix this without breaking code, which depends on the current order, in a non-obvious way. ------------------------------------------------------------------------ [2012-05-02 22:16:24] 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'. ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=61915&edit=1

« previous php.bugs (#220378) next »