ID:               36832
 Updated by:       [EMAIL PROTECTED]
 Reported By:      rdoggett at oz dot net
-Status:           Open
+Status:           Closed
 Bug Type:         Documentation problem
 Operating System: any
 PHP Version:      5.1.2
 New Comment:

This is already stated in the documentation:

http://ch2.php.net/language.operators.comparison#language.operators.comparison.ternary


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

[2006-03-23 12:59:47] rdoggett at oz dot net

Description:
------------
The ternery operator causes grief because its parsing precedence is
makedly different from other languages, as shown in the attached code.

One could reasonably expect the output to be "1 < 2", and indeed this
is the case when translated to perl, c, or javascript.  It is difficult
to believe that php's behavior could be an intentional language
feature.

The problem can be solved by inserting nested parenthesis, so the
parser's notion of operator precedence is implicated.

Although changing it to work more like other languages *could* break
some existing php code, this seems very unlikely.  It could actually
fix some unfound bugs.

Please, if you don't change the implementation, at least make the
manual explain how the ternery operator's parsing precedence works.
(with a big warning)



Reproduce code:
---------------
<?
$p = 1;
$out =  ($p < 2) ?  '< 2' :
        ($p > 4) ?  '> 4' :
                    '= $p';
echo "$p $out\n";
?>

Expected result:
----------------
1 < 2

Actual result:
--------------
1 > 4


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


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

Reply via email to