473,320 Members | 1,909 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

true?2:false?3:4 - is it a bug?

Why does the following script output 3? Can anyone explain that?

<?php
echo true? 2: false? 3: 4 ;
?>

Thanks,
-Marcin
Jul 17 '05 #1
4 1594
On Sat, 21 May 2005 09:24:55 +0200, Marcin wrote:
Why does the following script output 3? Can anyone explain that?
echo true? 2: false? 3: 4 ;


It is evaluated from left to right like so

echo ((true ? 2 : false) ? 3 : 4);
echo (2 ? 3 : 4);
echo 3;
--
Firefox Web Browser - Rediscover the web - http://getffox.com/
Thunderbird E-mail and Newsgroups - http://gettbird.com/
Jul 17 '05 #2
Interesting. In C, you would get 2,

Jul 17 '05 #3
In either language, you should use paranthesis, IMHO. Not using them
requires that one be familiar with the order in which the particular
language evaluates expressions (which, incidently, is something most
people probably aren't going to be familiar with - you weren't. I'm
not. etc).

Chung Leong <ch***********@hotmail.com> wrote:
Interesting. In C, you would get 2,


Jul 17 '05 #4
On 21 May 2005 22:04:33 -0700, "Chung Leong <ch***********@hotmail.com>"
<ch***********@hotmail.com> wrote:
Interesting. In C, you would get 2,


Indeed; PHP appears to have different results here to several other major
languages.

andyh@server:~/tmp/ternary$ cat ternary.c
#include <stdio.h>

#define TRUE 1
#define FALSE 0

int main()
{
printf("%d\n", TRUE ? 2 : FALSE ? 3: 4);
return 0;
}
andyh@server:~/tmp/ternary$ gcc -o ternary ternary.c
andyh@server:~/tmp/ternary$ ./ternary
2

andyh@server:~/tmp/ternary$ cat ternary.pl
#!/usr/bin/perl
print 1 ? 2 : 0 ? 3 : 4;
print "\n";
andyh@server:~/tmp/ternary$ ./ternary.pl
2

andyh@server:~/tmp/ternary$ cat ternary.php
<?php
echo true ? 2 : false ? 3 : 4;
echo "\n";
?>
andyh@server:~/tmp/ternary$ php -q ternary.php
3
The issue is operator associativity; Perl (and C) implements the ternary
operator as right-associative, whereas PHP implements it as left-associative.

This is documented in the Operators section in the PHP manual so it might be
hard to call it a bug, but it seems to be at least a design mistake to have
implemented it with the opposite associativity of the "original" languages.

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

46
by: Scott Chapman | last post by:
There seems to be an inconsistency here: Python 2.3.2 (#1, Oct 3 2003, 19:04:58) on linux2 >>> 1 == True True >>> 3 == True False >>> if 1: print "true" ....
3
by: drs | last post by:
I just upgraded my Python install, and for the first time have True and False rather than 1 and 0. I was playing around at the command line to test how they work (for instance, "if 9:" and "if...
35
by: Steven Bethard | last post by:
I have lists containing values that are all either True, False or None, e.g.: etc. For a given list: * If all values are None, the function should return None.
14
by: Walter Dnes (delete the 'z' to get my real address | last post by:
I took a C course some time ago, but I'm only now beginning to use it, for a personal pet project. My current stumbling-block is finding an efficient way to find a match between the beginning of a...
48
by: Skybuck Flying | last post by:
Hi, I came across this C code which I wanted to understand etc it looked like this: if (-1) etc It made me wonder what the result would be... true or false ? In C and Delphi
1
by: Edward | last post by:
I am having a terrible time getting anything useful out of a listbox on my web form. I am populating it with the results from Postcode lookup software, and it is showing the results fine. What...
4
by: Wayne Wengert | last post by:
I am using VB in a VSNET 2003 Windows application. I've run into a situation where, when trying to set a bit value in a SQL Server 2000 database I get errors because the values extracted from a...
59
by: Pierre Quentel | last post by:
Hi all, In some program I was testing if a variable was a boolean, with this test : if v in My script didn't work in some cases and I eventually found that for v = 0 the test returned True ...
30
by: Jason | last post by:
I am fairly new to ASP--I have been using it about 2 months. I did these tests (below), and it doesn't make sense to me. False is equal to 0, and that's fine. True should be equal to 1, but it's...
71
by: David T. Ashley | last post by:
Where is the best place to define TRUE and FALSE? Are they in any of the standard include files, ever? Do any standards apply? What I've traditionally done is something like: #ifndef...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.