php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #44179 Ternary Operator Does Not Accept Alpha Logical Operators
Submitted: 2008-02-20 04:30 UTC Modified: 2008-02-20 20:17 UTC
From: rpanning at hotmail dot com Assigned:
Status: Not a bug Package: Unknown/Other Function
PHP Version: 5.3CVS-2008-02-20 (snap) OS: XP Pro SP2
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: rpanning at hotmail dot com
New email:
PHP Version: OS:

 

 [2008-02-20 04:30 UTC] rpanning at hotmail dot com
Description:
------------
It seems that the ternary conditional operator does not accept the text logical operators (and, or, xor). They assign int 1 instead of the other expressions.

The xor is even odder in that if the TRUE comes before the xor, it will not assign anything. If it is the reverse, it will assign int 1.

Tested this with both PHP 5.2.5 and the latest snap of 5.3. Don't believe this is the expected behavior.

Reproduce code:
---------------
$and = (TRUE and TRUE  ? 'True' : 'False');
$or  = (FALSE or TRUE  ? 'True' : 'False');
$xor = (TRUE xor FALSE ? 'True' : 'False');
$aa  = (TRUE && TRUE   ? 'True' : 'False');
$ll  = (TRUE || FALSE  ? 'True' : 'False');

print('and = ' . $and . "<br>\r\n");
print('or  = '  . $or  . "<br>\r\n");
print('xor = ' . $xor . "<br>\r\n");
print('&&  = '  . $aa  . "<br>\r\n");
print('||  = '  . $ll  . "<br>\r\n");

Expected result:
----------------
and = True<br>
or  = True<br>
xor = True<br>
&&  = True<br>
||  = True<br>

Actual result:
--------------
and = 1<br>
or  = 1<br>
xor = <br>
&&  = True<br>
||  = True<br>

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-02-20 05:11 UTC] rasmus@php.net
Please check the operator precedence documentation.  The whole point of 'and', 'or' and 'xor' is that they are right at the bottom of the precedence table.  Only the ',' operator is below them.

See: http://php.net/manual/en/language.operators.php
 [2008-02-20 14:03 UTC] rpanning at hotmail dot com
Why does the operator precedence matter? They still don't work. Shouldn't "and" function the same as "&&"? The only time operator precedence would matter is if there were multiple operators in the expression. In this example there is only one for each expression, so operator precedence shouldn't come into play.
 [2008-02-20 17:44 UTC] rasmus@php.net
What do you mean why does it matter?

TRUE and TRUE ? 'True' : 'False'

when applying the precedence can be rewritten as:

TRUE and (TRUE ? 'True' : 'False')

which becomes

TRUE and 1

which equals 1

So it makes perfect sense, and no, 'and' and '&&' are obviously not exactly the same.  Why would we have two identical operators?  They differ in their precedence level as you have discovered.  That lets you do things like:

$a = $b + $c or fail();

Since the 'or' is of such low precedence the entire expression to the left will be evaluated first and only if that expression is false will the fail() function be called.  If you used || instead then $c would be OR'ed with the return value of the fail() function.
 [2008-02-20 20:17 UTC] rpanning at hotmail dot com
I see what you are saying, "and", "or", and "xor" are lower than ? and : so the expression goes with ? before "and". The entire ternary operator is treated as one expression, not 3 divided by ? and : (as an if/else expression).

I was starting to use "and" instead of && because of readability. It seems odd that you cannot use "and" but I guess could be resolved with parenthesis.

Maybe this should be noted in the documentation. Would help with questions. Thanks
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Feb 05 06:01:32 2025 UTC