For Programmers: Free Programming Magazines  


Home > Archive > PHP Documentation > September 2006 > #38679 [Opn]: ternery operator ?:









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author #38679 [Opn]: ternery operator ?:
tony2001@php.net

2006-09-01, 7:57 am

ID: 38679
Updated by: tony2001@php.net
Reported By: php at rebel dot com dot au
Status: Open
-Bug Type: Scripting Engine problem
+Bug Type: Documentation problem
Operating System: any
PHP Version: 4.4.4
New Comment:

Reclassified as documentation problem.


Previous Comments:
------------------------------------------------------------------------

[2006-09-01 11:24:41] php at rebel dot com dot au

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/langua....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


------------------------------------------------------------------------


--
Edit this bug report at http://bugs.php.net/?id=38679&edit=1
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com