Reply to Re: short if else

Your name:

Reply:


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

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация