Reply to Re: short if else

Your name:

Reply:


Posted by Oli Filth on 12/28/05 18:06

David Haynes said the following on 28/12/2005 15:44:
> Oli Filth wrote:
>
>> David Haynes said the following on 28/12/2005 14:42:
>>
>>> Oli Filth wrote:
>>>
>>>> black francis said the following on 28/12/2005 14:37:
>>>>
>>>>> try:
>>>>>
>>>>> $do = ($test == 1) ? true : false;
>>>>
>>>> Well, that can be shortened to:
>>>>
>>>> $do = ($test == 1);
>>>>
>>> Which can be shortened to:
>>>
>>> $do = $test;
>>>
>>> unless $test = 0, $do will evaluate as false.
>>>
>>
>> Technically, it's not the same. In the first two examples, $do will
>> *always* be a boolean. But in your new version, $do could be anything,
>> dependent on what type $test is.
>>
>>
> I disagree. My version simply relies on the casting rules for int to
> boolean. The example where ($test == 1) implicitly says that $test is of
> type integer not boolean since we know it would be $do = ($test == true)
> if $test was boolean.
>

I'm not sure what you're disagreeing with.

$do = ($test == 1) ? true : false;
and
$do = ($test == 1);

are exactly equivalent in every respect.

Whereas:

$do = $test;

not only results in $do being an integer (or whatever type $test is),
but when evaluated as a boolean (e.g. if ($do) {...}), does not result
in the same success/failure conditions. In fact, all you're doing is
copying the value, so this version achieves nothing!



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

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