Zero and NULL
am 09.01.2007 14:04:31 von Paul Lautman
I have the following switch statement:
switch ($record->sub_page) {
case -1:
$this->page = 6;
$error_message = '
You appear to have already completed the
form.';
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?
TIA
Regards
Paul
Re: Zero and NULL
am 09.01.2007 14:07:35 von Shion
Paul Lautman wrote:
> I have the following switch statement:
>
> switch ($record->sub_page) {
> case -1:
> $this->page = 6;
> $error_message = '
You appear to have already completed the
> form.';
> 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.
--
//Aho
Re: Zero and NULL
am 09.01.2007 14:07:35 von Shion
Paul Lautman wrote:
> I have the following switch statement:
>
> switch ($record->sub_page) {
> case -1:
> $this->page = 6;
> $error_message = '
You appear to have already completed the
> form.';
> 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.
--
//Aho
Re: Zero and NULL
am 09.01.2007 14:14:21 von Paul Lautman
J.O. Aho wrote:
> Paul Lautman wrote:
>> I have the following switch statement:
>>
>> switch ($record->sub_page) {
>> case -1:
>> $this->page = 6;
>> $error_message = '
You appear to have already completed
>> the form.';
>> 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.
Re: Zero and NULL
am 09.01.2007 14:14:21 von Paul Lautman
J.O. Aho wrote:
> Paul Lautman wrote:
>> I have the following switch statement:
>>
>> switch ($record->sub_page) {
>> case -1:
>> $this->page = 6;
>> $error_message = '
You appear to have already completed
>> the form.';
>> 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.
Re: Zero and NULL
am 09.01.2007 14:57:17 von Heiko Richler
Paul Lautman wrote:
> I have the following switch statement:
>
> switch ($record->sub_page) {
> case -1:
> $this->page = 6;
> $error_message = '
You appear to have already completed the
> form.';
> 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
Re: Zero and NULL
am 09.01.2007 14:57:17 von Heiko Richler
Paul Lautman wrote:
> I have the following switch statement:
>
> switch ($record->sub_page) {
> case -1:
> $this->page = 6;
> $error_message = '
You appear to have already completed the
> form.';
> 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
Re: Zero and NULL
am 09.01.2007 15:00:48 von Umberto Salsi
"Paul Lautman" wrote:
> Is it correct that php treats NULL as 0?
The switch() statement uses the weak comparison operator ==.
When the weak operator == is used the reply is yes: 0==NULL.
> Can anyone suggest a good fix?
1. Avoid to mix different types in expressions: it's confusing and dangerous.
For example, ensure the expression inside the switch() be int: switch( (int)
(EXPR) ){ ... }. Avoid to use a string as selector because "09" == 9 ==
"0x9" ==... difficult to say what ever else can match.
2. Do not use the switch(), use if() instead with the strong comparison
operator === as in this example:
if(EXPR === 1){
}else if(EXPR === NULL){
}else{
}
Best regards,
___
/_|_\ Umberto Salsi
\/_\/ www.icosaedro.it
Re: Zero and NULL
am 09.01.2007 15:00:48 von Umberto Salsi
"Paul Lautman" wrote:
> Is it correct that php treats NULL as 0?
The switch() statement uses the weak comparison operator ==.
When the weak operator == is used the reply is yes: 0==NULL.
> Can anyone suggest a good fix?
1. Avoid to mix different types in expressions: it's confusing and dangerous.
For example, ensure the expression inside the switch() be int: switch( (int)
(EXPR) ){ ... }. Avoid to use a string as selector because "09" == 9 ==
"0x9" ==... difficult to say what ever else can match.
2. Do not use the switch(), use if() instead with the strong comparison
operator === as in this example:
if(EXPR === 1){
}else if(EXPR === NULL){
}else{
}
Best regards,
___
/_|_\ Umberto Salsi
\/_\/ www.icosaedro.it
Re: Zero and NULL
am 09.01.2007 15:12:51 von Paul Lautman
Heiko Richler wrote:
> if sub_page is 0 it sets page=5 ?
>
Yep it sure does.
Thanks for the hint on the way that switch comares work.
Wierd thing is that, whilst it seems to treat NULL as 0, if I put a NULL
test in befor ethe 0 one, it does not detect 0 as NULL.
So it seems NULL = 0 but 0 != NULL.
Re: Zero and NULL
am 09.01.2007 15:12:51 von Paul Lautman
Heiko Richler wrote:
> if sub_page is 0 it sets page=5 ?
>
Yep it sure does.
Thanks for the hint on the way that switch comares work.
Wierd thing is that, whilst it seems to treat NULL as 0, if I put a NULL
test in befor ethe 0 one, it does not detect 0 as NULL.
So it seems NULL = 0 but 0 != NULL.
Re: Zero and NULL
am 09.01.2007 15:28:12 von Shion
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 = '
You appear to have already completed
>>> the form.';
>>> 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
Re: Zero and NULL
am 09.01.2007 15:28:12 von Shion
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 = '
You appear to have already completed
>>> the form.';
>>> 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
Re: Zero and NULL
am 09.01.2007 15:57:04 von Paul Lautman
J.O. Aho wrote:
> 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 = '
You appear to have already completed
>>>> the form.';
>>>> 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;
> }
Hey, I like it! Thanks
Re: Zero and NULL
am 09.01.2007 15:57:04 von Paul Lautman
J.O. Aho wrote:
> 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 = '
You appear to have already completed
>>>> the form.';
>>>> 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;
> }
Hey, I like it! Thanks
Re: Zero and NULL
am 09.01.2007 17:00:29 von unknown
Post removed (X-No-Archive: yes)
Re: Zero and NULL
am 09.01.2007 17:00:29 von unknown
Post removed (X-No-Archive: yes)
Re: Zero and NULL
am 09.01.2007 17:13:15 von Paul Lautman
David Gillen wrote:
> Paul Lautman said:
>> Heiko Richler wrote:
>>> if sub_page is 0 it sets page=5 ?
>>>
>> Yep it sure does.
>>
>> Thanks for the hint on the way that switch comares work.
>>
>> Wierd thing is that, whilst it seems to treat NULL as 0, if I put a
>> NULL test in befor ethe 0 one, it does not detect 0 as NULL.
>>
>> So it seems NULL = 0 but 0 != NULL.
>>
>>
> Not if I run this block of code.
>
> if(0 != NULL)
> {
> echo "0 is NOT equal to NULL
\n";
> }
> else
> {
> echo "0 is equal to NULL
\n";
> }
>
> if(NULL == 0)
> {
> echo "NULL is equal to 0
\n";
> }
> else
> {
> echo "NULL is not equal to 0
\n";
> }
>>
>
> Result is
> 0 is equal to NULL
> NULL is equal to 0
>
> D.
But I wasn't running that code. I was using switch.
Re: Zero and NULL
am 09.01.2007 17:13:15 von Paul Lautman
David Gillen wrote:
> Paul Lautman said:
>> Heiko Richler wrote:
>>> if sub_page is 0 it sets page=5 ?
>>>
>> Yep it sure does.
>>
>> Thanks for the hint on the way that switch comares work.
>>
>> Wierd thing is that, whilst it seems to treat NULL as 0, if I put a
>> NULL test in befor ethe 0 one, it does not detect 0 as NULL.
>>
>> So it seems NULL = 0 but 0 != NULL.
>>
>>
> Not if I run this block of code.
>
> if(0 != NULL)
> {
> echo "0 is NOT equal to NULL
\n";
> }
> else
> {
> echo "0 is equal to NULL
\n";
> }
>
> if(NULL == 0)
> {
> echo "NULL is equal to 0
\n";
> }
> else
> {
> echo "NULL is not equal to 0
\n";
> }
>>
>
> Result is
> 0 is equal to NULL
> NULL is equal to 0
>
> D.
But I wasn't running that code. I was using switch.
Re: Zero and NULL
am 09.01.2007 17:47:14 von unknown
Post removed (X-No-Archive: yes)
Re: Zero and NULL
am 09.01.2007 17:47:14 von unknown
Post removed (X-No-Archive: yes)