Code Comments
Programming Forum and web based access to our favorite programming groups.Look at the operator ternary: $flag = true; ($flag)?($a = "yes"):($a = "no"); Now i want to have multiple statements in my operator ternary something like (does not work!!!): ($flag)?($a = "yes";$b="yes"):($a = "no";$b="no"); One way to make this work is: ($flag)?($a = "yes" and $b="yes"):($a = "no" and $b="no"); but to me this is not logical. Does somebody have any ideas about this? regards, Marcel
Post Follow-up to this messageMarcel said the following on 02/06/2005 10:21:
> Look at the operator ternary:
>
> $flag = true;
>
> ($flag)?($a = "yes"):($a = "no");
>
> Now i want to have multiple statements in my operator ternary something li
ke
> (does not work!!!):
>
The ternary operator is not designed to execute code. It's designed for
uses such as:
$a = $flag : "yes" : "no";
i.e. assigning a value dependent on the logical state of $flag. It just
so happens that your code above works, because '$a = "yes"' evaluates to
a value, and so can be used in the ternary operator.
Why not just use if? That's what it's there for! And it's far more
obvious what's going on than trying to cram a load of code onto one line.
if ($flag)
{
..
}
else
{
..
}
--
Oli
Post Follow-up to this message
"Oli Filth" <catch@olifilth.co.uk> schreef in bericht
news:bvAne.4425$cN2.1263@newsfe4-gui.ntli.net...
> Marcel said the following on 02/06/2005 10:21:
>
> The ternary operator is not designed to execute code. It's designed for
> uses such as:
>
> $a = $flag : "yes" : "no";
>
> i.e. assigning a value dependent on the logical state of $flag. It just so
> happens that your code above works, because '$a = "yes"' evaluates to a
> value, and so can be used in the ternary operator.
>
> Why not just use if? That's what it's there for! And it's far more obvious
> what's going on than trying to cram a load of code onto one line.
>
> if ($flag)
> {
> ...
> }
> else
> {
> ...
> }
>
>
Ok thanks that what i was thinking too!
Regards,
Marcel
Post Follow-up to this message"Marcel" <sorryafraidofspam@nospam.com> wrote in message news:de175$429ecf8b$3e3a8507$17716@news.versatel.nl... > Look at the operator ternary: > > $flag = true; > > ($flag)?($a = "yes"):($a = "no"); > > Now i want to have multiple statements in my operator ternary something > like (does not work!!!): > > ($flag)?($a = "yes";$b="yes"):($a = "no";$b="no"); > > One way to make this work is: > > ($flag)?($a = "yes" and $b="yes"):($a = "no" and $b="no"); but to me this > is not logical. > > Does somebody have any ideas about this? > You're supposed to use it like this: $a = $flag ? 'yes' : 'no'; $b = $flag ? 'yes' : 'no'; $c = $flag ? 'michael' : 'knight'; etc.. Or like Oli said, use if statement... -- Welcome to Usenet! Please leave tolerance, understanding and intelligence at the door. They aren't welcome here. eternal piste erection miuku gmail piste com
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.