Posted by Heiko Richler on 01/09/07 13:57
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.
select in php uses == and 0 == null seams to be true
> 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.
if sub_page is 0 it sets page=5 ?
> Is it correct that php treats NULL as 0?
Yes, as == does.
> Can anyone suggest a good fix?
Use if and ===:
if ($record->sub_page === 6) ...
elseif ($record->sub_page === 5) ...
elseif ($record->sub_page === 1) ...
elseif ($record->sub_page === 0) ...
elseif ($record->sub_page === null) ...
else ...
Heiko
--
http://portal.richler.de/ Namensportal zu Richler
http://www.richler.de/ Heiko Richler: Computer - Know How!
http://www.richler.info/ private Homepage
[Back to original message]
|