PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > alt.php > ?:
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
?:

Réponse
 
LinkBack Outils de la discussion
Vieux 06/12/2007, 13h17   #1
newbie
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut ?:

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.
  Réponse avec citation
Vieux 06/12/2007, 13h24   #2
newbie
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: ?:

newbie wrote:
$x = 40; should stand before echo in my last post.
  Réponse avec citation
Vieux 06/12/2007, 15h24   #3
Janwillem Borleffs
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: ?:

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
  Réponse avec citation
Vieux 06/12/2007, 16h43   #4
newbie
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: ?:

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.
  Réponse avec citation
Vieux 06/12/2007, 17h14   #5
Tim Streater
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: ?:

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/

??
  Réponse avec citation
Vieux 06/12/2007, 21h29   #6
Janwillem Borleffs
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: ?:

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


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 06h35.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14072 seconds with 14 queries