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?
TIA
Regards
Paul 13 2539
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.
--
//Aho
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.
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
"Paul Lautman" <pa**********@btinternet.comwrote:
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
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.
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
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 = '<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;
}
Hey, I like it! Thanks
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<br>\n";
}
else
{
echo "0 is equal to NULL<br>\n";
}
if(NULL == 0)
{
echo "NULL is equal to 0<br>\n";
}
else
{
echo "NULL is not equal to 0<br>\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.
Paul Lautman wrote:
I have the following switch statement:
Is it correct that php treats NULL as 0?
Can anyone suggest a good fix?
Stuckle had a great little one-line if-then to deal with that.
He posted it for me a few days ago, but I didn't save it.
I was gonna axe him to repost it, and this is a good opportunity.
Would you post it again, Herr Stuckle?
Why don't we instead tweak the whole concept of magic numbers? And
don't use 0 there at all - but something else, 1&2, or.
static $STATE_ALREADY_COMPLETED = 1152; // Or whatever, was -1
static $STATE_COMPLETE_NOW = 1153; // Or whatever, was 0
switch($record->sub_page) {
case $STATE_ALREADY_COMPLETED:
$this->page = 6;
$error_message = '<center>You appear to have already completed the
form.</center>';
break;
case $STATE_COMPLETE_NOW:
$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
}
This solution is totally independent of what is the meaning of '-1' or
'0'. If you need to add more states, with whatever meaning, would you
prefer doing:
case 3:
doSomething2();
case 4:
doAnotherFunkyThing();
case 5:
whatTheHeckDoesFiveMean();
or
case $STATE_DO_SOMETHING:
doSomething2();
case $STATE_GET_FUNKY:
doAnotherFunkyThing();
case $STATE_FURIOUS_FIVE:
...
..oO(ju*****@gmail.com)
>Why don't we instead tweak the whole concept of magic numbers? And don't use 0 there at all - but something else, 1&2, or.
static $STATE_ALREADY_COMPLETED = 1152; // Or whatever, was -1 static $STATE_COMPLETE_NOW = 1153; // Or whatever, was 0
define('STATE_ALREADY_COMPLETED', 1152);
define('STATE_COMPLETE_NOW', 1153);
Micha
Michael Fesser wrote:
.oO(ju*****@gmail.com)
Why don't we instead tweak the whole concept of magic numbers? And
don't use 0 there at all - but something else, 1&2, or.
static $STATE_ALREADY_COMPLETED = 1152; // Or whatever, was -1
static $STATE_COMPLETE_NOW = 1153; // Or whatever, was 0
define('STATE_ALREADY_COMPLETED', 1152);
define('STATE_COMPLETE_NOW', 1153);
Yeah! Fesser lead the way! :-)
That's the way to do IT.
That my part is for classes, right? Been working mostly with java
-lately.
t.j
..oO(ju*****@gmail.com)
>That my part is for classes, right?
In classes you can use the 'const' keyword for defining class constants.
>Been working mostly with java
Yep. ;)
But, unlike in Java, in PHP variables and constants are two different
things.
Micha This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Margaret MacDonald |
last post by:
Probably it's something dumb that I'm doing/not doing, but echo
doesn't seem to want to echo a literal zero when the value of some var
is zero. E.g.:
$foo = 0 ;
echo 'foo is ' . $foo ;
...
|
by: Rob Fielding |
last post by:
Hi,
We're currently experiencing a problem where SQL statements are failing
when entring a '' for not not-null integer columns:
ERROR: pg_atoi: zero-length string
This was discovered just...
|
by: RC |
last post by:
I have a form with five text boxes on it. The format for all the boxes
is set as General Number. In four of the boxes the user can enter a
number, the fifth box totals up the values in the other...
|
by: Zhiqiang Ye |
last post by:
Hi, All
I am reading FAQ of this group. I have a question about this:
http://www.eskimo.com/~scs/C-faq/q7.31.html
It says:
"
p = malloc(m * n);
memset(p, 0, m * n);
The zero fill is...
|
by: 69dbb24b2db3daad932c457cccfd6 |
last post by:
Hello,
I have to initialize all elements of a very big float point array to
zero. It seems memset(a, 0, len) is faster than a simple loop. I just
want to know whether it is safe to do so, since I...
|
by: pm940 |
last post by:
Hello.
I've been reading some past discussions on the NULL vs. zero.
References are always made to systems or machienes that use values
other than zero to represent the NULL pointer.
Although...
|
by: Lyle Fairfield |
last post by:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbaac11/html/acfctNZ_HV05186465.asp
"If the value of the variant argument is Null, the Nz function returns
the number zero or a...
|
by: Robert Seacord |
last post by:
The C standard doesn't say anything about what happens when you call
realloc with a size argument of 0. Both glibc and openbsd appear to
return a valid pointer to a zero-sized object.. e.g. the...
|
by: lovecreatesbea... |
last post by:
Do you prefer malloc or calloc?
p = malloc(size);
Which of the following two is right to get same storage same as the
above call?
p = calloc(1, size);
p = calloc(size, 1);
|
by: Kenneth Brody |
last post by:
I looked at my copy of n1124, and I didn't see anything about this
particular situation...
What happens if you realloc() to a size of zero?
Implementations are allowed to return NULL on...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |