?:

?:

am 06.12.2007 14:17:09 von newbie

Can someone explain to me why the following code yields output "20 < x
<= 30"?

echo $x > 30 ? "x > 30" : $x > 20 ? "20 < x <= 30" : "x < 20";


I expected that ir will be evaluated as ($x > 30 ? "x > 30" : ($x > 20 ?
"20 < x <= 30" : "x < 20")) but I was wrong.

Re: ?:

am 06.12.2007 14:24:03 von newbie

newbie wrote:
$x = 40; should stand before echo in my last post.

Re: ?:

am 06.12.2007 16:24:22 von Janwillem Borleffs

newbie schreef:
> Can someone explain to me why the following code yields output "20 < x
> <= 30"?
>
> echo $x > 30 ? "x > 30" : $x > 20 ? "20 < x <= 30" : "x < 20";
>
>
> I expected that ir will be evaluated as ($x > 30 ? "x > 30" : ($x > 20 ?
> "20 < x <= 30" : "x < 20")) but I was wrong.

It is evaluated from left to right:

If x > 30
print 'x > 30'
Else
If x > 20
print '20 < x <= 30'
Else
print 'x < 20'
End If
End If


JW

Re: ?:

am 06.12.2007 17:43:15 von newbie

Janwillem Borleffs wrote:
> ...newbie schreef:

Thank you for your reply, but I believe you're mistaken.

According to your code, for the $x = 40 I should receive output "x > 30"
(according to the first if clause in your code). But, actually I
receive the unexpected output of "20 < x <= 30".


C code outputs "x > 30":
int x = 40;
printf("%s", x > 30 ? "x > 30" : x > 20 ? "20 < x <= 30" : "x < 20");

JavaScript code outputs "x > 30":
x = 40;
document.write(x > 30 ? "x > 30" : x > 20 ? "20 < x <= 30" : "x < 20");

PHP code outputs "20 < x <= 30":
$x = 40;
echo $x > 30 ? "x > 30" : $x > 20 ? "20 < x <= 30" : "x < 20";



In the meanwhile I Googled for the explanation and it appears that PHP
example is evaluated as follows:
echo ($x > 30 ? "x > 30" : $x > 20) ? "20 < x <= 30" : "x < 20";


I tried to transform ?: to if clauses and this is what I get:
if ($x > 30) $foo = "x > 30";
else $foo = "x > 20";
if ($foo) echo "20 < x <= 30";
else echo "x < 20";

This explains why I get the (for me unexpected) result "20 < x <= 30".

This is no a real-world example. I'm just courious why PHP acts
different from some other languages.

Re: ?:

am 06.12.2007 18:14:13 von Tim Streater

In article , newbie
wrote:

> Janwillem Borleffs wrote:
> > ...newbie schreef:
>
> Thank you for your reply, but I believe you're mistaken.
>
> According to your code, for the $x = 40 I should receive output "x > 30"
> (according to the first if clause in your code). But, actually I
> receive the unexpected output of "20 < x <= 30".
>
>
> C code outputs "x > 30":
> int x = 40;
> printf("%s", x > 30 ? "x > 30" : x > 20 ? "20 < x <= 30" : "x < 20");
>
> JavaScript code outputs "x > 30":
> x = 40;
> document.write(x > 30 ? "x > 30" : x > 20 ? "20 < x <= 30" : "x < 20");
>
> PHP code outputs "20 < x <= 30":
> $x = 40;
> echo $x > 30 ? "x > 30" : $x > 20 ? "20 < x <= 30" : "x < 20";
>
>
>
> In the meanwhile I Googled for the explanation and it appears that PHP
> example is evaluated as follows:
> echo ($x > 30 ? "x > 30" : $x > 20) ? "20 < x <= 30" : "x < 20";
>
>
> I tried to transform ?: to if clauses and this is what I get:
> if ($x > 30) $foo = "x > 30";
> else $foo = "x > 20";
> if ($foo) echo "20 < x <= 30";
> else echo "x < 20";
>
> This explains why I get the (for me unexpected) result "20 < x <= 30".
>
> This is no a real-world example. I'm just courious why PHP acts
> different from some other languages.

Presumably you checked at:

http://www.php.net/manual/en/

??

Re: ?:

am 06.12.2007 22:29:43 von Janwillem Borleffs

newbie wrote:
> Thank you for your reply, but I believe you're mistaken.
>

Indeed I am, as the second note on the following page describes:

http://www.php.net/operators.comparison


JW