|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
newbie wrote:
$x = 40; should stand before echo in my last post. |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
In article <fj98nq$h5h$1@ss408.t-com.hr>, newbie <newbie@newbie.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/ ?? |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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 |
|
![]() |
| Outils de la discussion | |
|
|