Reply to Re: Zero and NULL

Your name:

Reply:


Posted by J.O. Aho on 01/09/07 14:28

Paul Lautman wrote:
> J.O. Aho wrote:
>> Paul Lautman wrote:
>>> I have the following switch statement:
>>>
>>> switch ($record->sub_page) {
>>> case -1:
>>> $this->page = 6;
>>> $error_message = '<center>You appear to have already completed
>>> the form.</center>';
>>> break;
>>> case 0:
>>> $this->page = 5;
>>> ff_query("UPDATE #__gateway_form_1 SET `sub_page`=-1 WHERE
>>> userid=$my->id and `expiry_date` = '$expiry_date'");
>>> break;
>>> default:
>>> do something else
>>> }
>>>
>>> The value of $record->sub_page can be NULL. If it is NULL, the case
>>> for 0 seems to fire. I really need the default action to happen when
>>> $record->sub_page is NULL or a +ve value.
>>>
>>> However if I do:
>>>
>>> switch ($record->sub_page) {
>>> case -1:
>>> $this->page = 6;
>>> break;
>>> case NULL:
>>> do something else;
>>> break;
>>> case 0:
>>> $this->page = 5;
>>> break;
>>> default:
>>> do something else;
>>> }
>>>
>>> then the switch manages to tell the difference between a NULL and a
>>> zero. Is it correct that php treats NULL as 0?
>>> Can anyone suggest a good fix?
>> NULL and 0 are many times treated as false, so you do need to take
>> care of things and check NULL/0/false.
>
> But I need the default action to happen if $record->sub_page is NULL.
>
> If I test for NULL before testing for 0 then all is detected fine. However
> if I test for NULL after testing for 0 then it fires the 0 action when it
> sees NULL. I don't really want to copy the default actions to the NULL
> actions too.
>
>
This should work, you set 0 as the next last option in the switch and default
as last (as it should), then checking for null in the case 0 should allow you
to distinguish it from NULL.

switch($something) {

case 0:
if(!is_null($something)) {
/* do what you need for 0 */
break;
}
/* No break for NULL we go automatically to the next one */
default:
break;
}

--

//Aho

[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

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