php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #38679 ternery operator ?:
Submitted: 2006-09-01 11:24 UTC Modified: 2006-09-01 15:34 UTC
From: php at rebel dot com dot au Assigned: sean (profile)
Status: Closed Package: Documentation problem
PHP Version: 4.4.4 OS: any
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: php at rebel dot com dot au
New email:
PHP Version: OS:

 

 [2006-09-01 11:24 UTC] 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/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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-09-01 11:37 UTC] tony2001@php.net
Reclassified as documentation problem.
 [2006-09-01 13:12 UTC] php at rebel dot com dot au
Reclassifying this bug report as a document problem is somewhat akin to sweeping the dirt under the carpet.
 [2006-09-01 15:34 UTC] sean@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.

The manual already stated that ternary expressions are evaluated left-to-right. However, I agree: this behaviour is confusing. I added a note that will help clear this up.

S

 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Mon Jul 14 21:01:33 2025 UTC