php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #25356 ?: (ternary) - shold operate as in C, but does not
Submitted: 2003-09-02 06:30 UTC Modified: 2004-04-08 00:26 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: matschek at gmx dot de Assigned:
Status: Closed Package: Documentation problem
PHP Version: Irrelevant OS: Irrelevant
Private report: No CVE-ID: None
 [2003-09-02 06:30 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-09-02 08:24 UTC] sniper@php.net
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.

 [2004-04-08 00:26 UTC] irchtml@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.

Removed note.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 16:01:29 2024 UTC