#25356 [Opn->Ver]: ?: (ternary) - shold operate as in C, but does not
| From: | sniper@php.net | Date: | Tue, 02 Sep 2003 13:24:49 +0000 |
| Subject: | #25356 [Opn->Ver]: ?: (ternary) - shold operate as in C, but does not | ||
| References: | 1 | Groups: | php.doc |
| Request: | Send a blank email to phpdoc+get-969356178@lists.php.net to get a copy of this message | ||
ID: 25356
Updated by: sniper@php.net
Reported By: matschek at gmx dot de
-Status: Open
+Status: Verified
Bug Type: Documentation problem
Operating System: Irrelevant
PHP Version: Irrelevant
New Comment:
To get the correct result do this:
echo $c1 ? 1 : ($c2 ? 2 : 3);
Isn't that much readable too? :)
Anyway, that note about ternary being same as in C is wrong.
Previous Comments:
------------------------------------------------------------------------
[2003-09-02 06:30:51] matschek at gmx dot de
Description:
------------
The documentation says in the section Comparison Operators:
"Another conditional operator is the "?:" (or ternary) operator, which
operates as in C and many other languages"
This is not true, because in C and other languages the ternary-operator
is parsed/compiled from left to right, and therefore cases other
results.
The included code returns "1" in C and JavaScript, for example, but "2"
in PHP
Reproduce code:
---------------
$c1=true;
$c2=false;
echo $c1 ? 1 : $c2 ? 2 : 3;
Expected result:
----------------
1
Actual result:
--------------
2
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=25356&edit=1