From:             php at rebel dot com dot au
Operating system: any
PHP version:      4.4.4
PHP Bug Type:     Scripting Engine problem
Bug description:  ternery operator ?:

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

-- 
Edit bug report at http://bugs.php.net/?id=38679&edit=1
-- 
Try a CVS snapshot (PHP 4.4): 
http://bugs.php.net/fix.php?id=38679&r=trysnapshot44
Try a CVS snapshot (PHP 5.2): 
http://bugs.php.net/fix.php?id=38679&r=trysnapshot52
Try a CVS snapshot (PHP 6.0): 
http://bugs.php.net/fix.php?id=38679&r=trysnapshot60
Fixed in CVS:                 http://bugs.php.net/fix.php?id=38679&r=fixedcvs
Fixed in release:             
http://bugs.php.net/fix.php?id=38679&r=alreadyfixed
Need backtrace:               http://bugs.php.net/fix.php?id=38679&r=needtrace
Need Reproduce Script:        http://bugs.php.net/fix.php?id=38679&r=needscript
Try newer version:            http://bugs.php.net/fix.php?id=38679&r=oldversion
Not developer issue:          http://bugs.php.net/fix.php?id=38679&r=support
Expected behavior:            http://bugs.php.net/fix.php?id=38679&r=notwrong
Not enough info:              
http://bugs.php.net/fix.php?id=38679&r=notenoughinfo
Submitted twice:              
http://bugs.php.net/fix.php?id=38679&r=submittedtwice
register_globals:             http://bugs.php.net/fix.php?id=38679&r=globals
PHP 3 support discontinued:   http://bugs.php.net/fix.php?id=38679&r=php3
Daylight Savings:             http://bugs.php.net/fix.php?id=38679&r=dst
IIS Stability:                http://bugs.php.net/fix.php?id=38679&r=isapi
Install GNU Sed:              http://bugs.php.net/fix.php?id=38679&r=gnused
Floating point limitations:   http://bugs.php.net/fix.php?id=38679&r=float
No Zend Extensions:           http://bugs.php.net/fix.php?id=38679&r=nozend
MySQL Configuration Error:    http://bugs.php.net/fix.php?id=38679&r=mysqlcfg

Reply via email to