|
Posted by Oli Filth on 12/28/05 16:52
Moneypenny said the following on 28/12/2005 10:51:
> Hy,
>
> I've got an short ifelse notation, but can give only one action like:
>
> $test == 1 ? $do = true : $do = false;
>
> But this gives an parse error:
>
> $test == 1 ? $do = true $who = "me" : $do = false;
> or
> $test == 1 ? $do = true; $who = "me"; : $do = false;
>
> All gives a parse error.
>
> How can i do this?
>
Use a standard if-else. The ternary operator ?: is *not*, repeat *not*,
a short-hand form for if() {...} else {...}.
It's designed to be used to evaluate a value that's dependent on some
condition, i.e.:
$result = ($test_expression) ? $val_if_true : $val_if_false;
Now, it just so happens that assignments (e.g. $do = true in your
example above) resolve to a value, and so can be used as $val_if_true or
$val_if_false. Don't let that fool you into thinking that you can
execute arbitrary blocks of code inside a ternary statement.
--
Oli
Navigation:
[Reply to this message]
|