RE: [PHP] Re: question about ? :
| From: | Ford, Mike [LSS] | Date: | Wed, 19 Jun 2002 19:38:16 +0000 |
| Subject: | RE: [PHP] Re: question about ? : | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-102998@lists.php.net to get a copy of this message | ||
> -----Original Message-----
> From: Lee Doolan [mailto:lee@affero.com]
> Sent: 19 June 2002 17:08
> To: php-general@lists.php.net
> Subject: [PHP] Re: question about ? :
>
>
> >>>>> "Mike" == Mike Ford <M.Ford@lmu.ac.uk> writes:
>
> >> -----Original Message----- From: Joel Boonstra
> >> [mailto:jboonstra@gospelcom.net] Sent: 19 June 2002 14:00
> >>
> >> > OK, try this: why does
> >> >
> >> > $x = 3 + 5 - 7;
> >> >
> >> > produce different results from
> >> >
> >> > $x = 3 + (5 - 7);
>
> I still dont understand why something like
>
> <expr-a> ? <expr-b> : <expr-c>
>
> should evaluate differently than
>
> <expr-a> ? <expr-b> : (<expr-c>)
>
> where <expr-c> is ternary. consult the original
> question for an example.
OK, we need to expand that slightly to get a double-ternary into the mix, if
you see what I mean:
a ? x : c ? y : z
Now, ?: is a left-associative operator in PHP, so this is equivalent to:
(a ? x : c) ? y : z
or, in other words: if a is true then use the Boolean value of x, else use
the Boolean value of c, to decide whether to return y or z.
Your other alternative is:
a ? x : (c ? y : z)
which is quite different (if a is true return x, otherwise use the Boolean
value of c to decide whether to return y or z)!
OK?
Cheers!
Mike
---------------------------------------------------------------------
Mike Ford, Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Beckett Park, LEEDS, LS6 3QS, United Kingdom
Email: m.ford@lmu.ac.uk
Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211