CASE: NULL [SOLVED]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tomprogers
Forum Commoner
Posts: 50
Joined: Fri Mar 17, 2006 5:17 pm
Location: Minnesota
Contact:

CASE: NULL [SOLVED]

Post by tomprogers »

I have a boolean field in my database. Based on the value of the field (NULL/0/1), I want to take different actions. I use a switch, like so:

Code: Select all

switch($boolean)
case 0: /* do something for records that have been assigned a 0 value */ break;
case 1: /* do something for records that have been assigned a 1 value */ break;
case NULL: /* do something for records that have not been assigned a value yet */ break;
Unfortunately, it seems that a DB value of NULL is not being converted to PHP's NULL, and is instead matching the 0 case. This means I can't replace my case NULL with a plain default either - NULL will still trip the case 0. I checked the pg_fetch_array documentation, and it said that it is supposed to be properly preserving NULL values.

Am I overlooking something, or can I not use a switch?


Edit: I tried explicitly setting $var = NULL and switching on it, and it still equated 0 with NULL, regardless of the order of the case statements.
Last edited by tomprogers on Tue May 02, 2006 2:46 pm, edited 1 time in total.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Null technically means is not set, so switch cannot switch on it and the check should probably be:

Code: Select all

if(isset($boolean)) {
    switch($boolean) {
    case 0: /* do something for records that have been assigned a 0 value */ break;
    case 1: /* do something for records that have been assigned a 1 value */ break;
    }
} else {
    /* do something for records that have not been assigned a value yet */ break; 
}
Which is:

Code: Select all

if(isset($boolean)) {
    if ($boolean) {
        /* do something for records that have been assigned a 0 value */ break;
    } else {
        /* do something for records that have been assigned a 1 value */ break;
    }
} else {
    /* do something for records that have not been assigned a value yet */ break; 
}
But you would really need to see what is being returned by the fetch().
(#10850)
tomprogers
Forum Commoner
Posts: 50
Joined: Fri Mar 17, 2006 5:17 pm
Location: Minnesota
Contact:

Post by tomprogers »

Fair enough. I'd already switch to a if-else block with a ternary statement to keep things short, but it's good to know why this is a problem.

Thanks.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

tomprogers wrote:Fair enough. I'd already switch to a if-else block with a ternary statement to keep things short, but it's good to know why this is a problem.

Thanks.
<Opinion only>
Use ternary statements sparingly.... they can get messy and difficult to make sense of in larger scripts.
</Opinion>
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

<Opinion only>
Use ternary statements sparingly.... they can get messy and difficult to make sense of in larger scripts.
</Opinion>
I share that opinion.
Post Reply