|
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]
|