Reply to Re: ?:

Your name:

Reply:


Posted by newbie on 12/06/07 16:43

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.

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация