You are here: Re: please explain or refer: $string = isset($xyz) ? $xyz : "something else"; « PHP Programming Language « IT news, forums, messages
Re: please explain or refer: $string = isset($xyz) ? $xyz : "something else";

Posted by Jason F. on 05/11/05 07:08

Andrew DeFaria wrote:
> Jason F. wrote:
>
>> That's the ternary operator, which you'll find in a lot of languages
>> besides just PHP.
>>
>> The above example is exactly equivalent to:
>>
>> if (isset($xyz)) {
>> $string = $xyz;
>> } else {
>> $string = "something else";
>> }
>>
>> In the interest of *easily readable code* I would recommend that you
>> try *NOT* to use the ternary much just to appear clever. Sure, it's
>> more compact, but it takes a couple extra seconds to figure out what's
>> going on VS an if/else.
>
>
> It takes a couple of extra seconds (actually about .5 seconds) the first
> few times. After that it's quite natural and no less confusing than the
> if you constructed above.

The ternary always takes extra time to grok, even if you've been
[ab]using it for years. Many agree that the Best Practice(TM) is to not
use it, or to at least limit its use to very simple cases. The worst are
the "clever" nested ternaries that span multiple lines.

 

Navigation:

[Reply to this 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

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