|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2006-09-01 11:37 UTC] tony2001@php.net
[2006-09-01 13:12 UTC] php at rebel dot com dot au
[2006-09-01 15:34 UTC] sean@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Nov 19 20:00:01 2025 UTC |
Description: ------------ As has been reported many times the ternary operator ?: does not operate in a logical fashion. I have seen this bug reported and returned as bogus. how can that be. the construct "expr1 ? expr2:expr3 ? expr4:expr5" should be semantically similar to if (expr1) expr2 else if (expr3) expr4 else expr5 Just because many people may rely on the incorrect operation of this operator does not mean it should not be fixed. (How can anyone rely on that weird bit of work, see test result below) And fixing it wouldnt break code that uses parenthesis to correct this bug. I know no one likes to admit they got it wrong but this is a particularly bad faux pas and should be corrected to bring this operator into line with what would be considered the norm. No other language I know that has this construct evaluates it in the way PHP does. even the documentation seems clear on how it should operate "The expression (expr1) ? (expr2) : (expr3) evaluates to expr2 if expr1 evaluates to TRUE, and expr3 if expr1 evaluates to FALSE." http://www.php.net/manual/en/language.operators.comparison.php There is nothing in that line of explanation that says if expr3 is not bracketed it will be evaluated TO THE EXCLUSION of expr1 which is obviously whats happening (see test results below) If there is a valid reason for the way this operator is evaluated please then explain so that I can pass on this perl of wisdom. Reproduce code: --------------- $true1 = false; $true2 = false; echo( $true1 ? 'true1':$true2 ? 'true2':'false' ); $true1 = true; $true2 = false; echo( $true1 ? 'true1':$true2 ? 'true2':'false' ); $true1 = false; $true2 = true; echo( $true1 ? 'true1':$true2 ? 'true2':'false' ); $true1 = true; $true2 = true; echo( $true1 ? 'true1':$true2 ? 'true2':'false' ); Expected result: ---------------- false true1 true2 true1 Actual result: -------------- false true2 true2 true2