Home > Archive > PHP Language > June 2005 > Operator ternary
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| Marcel 2005-06-02, 8:58 am |
| 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
| |
| Oli Filth 2005-06-02, 8:58 am |
| Marcel 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 like
> (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
| |
| Marcel 2005-06-02, 8:58 am |
|
"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
| |
| Kimmo Laine 2005-06-02, 8:58 am |
| "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
|
|
|
|
|