To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
PHPBuilder.com  
 

 

Go Back   PHPBuilder.com > PHP Help > Coding

Coding Help with PHP coding

Reply
 
Thread Tools Rate Thread Display Modes
Old 08-04-2005, 01:39 PM   #1
zeb
Senior Member
 
Join Date: Jun 2003
Location: Maine
Posts: 119
Differentiate between not set and NULL

Is there a good way to differentiate between variables or array indices set to NULL versus variable or array indices that haven't actually been set?

The only way I can see is to test if the variable or array index exists:
PHP Code:
if ( array_key_exists( 'indexBeingTested', $array ) )
    {
    echo
is_null( $array['indexBeingTested'] ) ? "is set and is NULL" : "is set and is NOT NULL";
    }
else
    {
    echo
"Never been set";
    }

// or for variables:
$allVars = get_defined_vars(); // or use $GLOBALS
if ( array_key_exists( 'variableName', $allVars ) )
    {
    echo
is_null( $variableName ) ? "is set and is NULL" : "is set and is NOT NULL";
    }
else
    {
    echo
"Never been set";
    }
Did I miss something, or is this the only way to distinguish between not set and NULL?
__________________
..:: Zeb ::..

zebellis.com
zeb is offline   Reply With Quote
Old 08-04-2005, 07:20 PM   #2
leatherback
Beware: Crazy Scientist
 
leatherback's Avatar
 
Join Date: Mar 2002
Location: Oxford (UK work), Melbourne (AU work), Vreden (DE partner), Zutphen (nl parents)
Posts: 5,183
Hm..

As far as I can tell NULL and not set are just about the same.

Out of curiosity.. Why would you want to differentiate?
__________________
- Codes provide logic/technique, not full working solutions
- Use: or die(mysql_error()) after each query and safe a headache
- use print_r($variable) to see the values in an array
------------------------
latest project|me|Coding tips
syntax editor|Get your question answered
leatherback is offline   Reply With Quote
Old 08-04-2005, 09:51 PM   #3
zeb
Senior Member
 
Join Date: Jun 2003
Location: Maine
Posts: 119
Right. They're the same except for the fact that a variable or index can be non-existent before it can be set to NULL. Those are two different states. They both test out the same conditionally in PHP, but the differences in those two states is undeniable. In one case you have a variable or index that hasn't entered your scripts execution, on the other hand you have a variable or index that has been executed, but has been given or deemed to be a NULL value.

So if you rely on a script that may or may not set a variable or array index (which would indicate whether or not that variable or index was in fact processed by that script), then testing for the difference between never set and set but NULL could be valuable. In my situation, it is a valuable bit of knowledge.

I think this distinction is more important and probably more relevant for array indices, but I threw in the variable condition just to be thorough (even though you end up testing the existence of variables in the same exact manner as the array indices).
__________________
..:: Zeb ::..

zebellis.com
zeb is offline   Reply With Quote
Old 08-05-2005, 02:56 AM   #4
mrhappiness
Senior Member
 
Join Date: Nov 2003
Location: kraut *g*
Posts: 1,378
what about isset?
__________________
The last good thing written in C was Schubert's symphony #9.
mrhappiness is offline   Reply With Quote
Old 08-05-2005, 08:49 AM   #5
zeb
Senior Member
 
Join Date: Jun 2003
Location: Maine
Posts: 119
Oh, c'mon! isset returns false for both unbound variables and for NULL variables. It does not differentiate between "never been set" and "set but NULL"
__________________
..:: Zeb ::..

zebellis.com
zeb is offline   Reply With Quote
Old 08-05-2005, 10:54 AM   #6
leatherback
Beware: Crazy Scientist
 
leatherback's Avatar
 
Join Date: Mar 2002
Location: Oxford (UK work), Melbourne (AU work), Vreden (DE partner), Zutphen (nl parents)
Posts: 5,183
Hi Zeb,

To avoid this sort of trouble, I usually have a specific value set for a nodata / not set value. e.g.: I never use -2 for anything, except in variable checking. Whenever I want a var ignored, I set it to -2, for which you can easily check.

?
__________________
- Codes provide logic/technique, not full working solutions
- Use: or die(mysql_error()) after each query and safe a headache
- use print_r($variable) to see the values in an array
------------------------
latest project|me|Coding tips
syntax editor|Get your question answered
leatherback is offline   Reply With Quote
Old 08-05-2005, 11:35 AM   #7
zeb
Senior Member
 
Join Date: Jun 2003
Location: Maine
Posts: 119
That's true, I could do that. It is easy to employ that sort of solution when you have a script serving a very specific purpose; but in terms of compatibility, expandability, and scalability, choosing something like that could be detrimental. The project I'm working on needs to have all those features. Where this need exists is in the core script into which many modules and extensions connect.

I was just wondering if there was any built in way of checking this.

mrhappiness: I just re-read my last post and realized that my tone was slightly harsh. That wasn't my intent. It was still early for me...
__________________
..:: Zeb ::..

zebellis.com
zeb is offline   Reply With Quote
Old 08-05-2005, 11:40 AM   #8
leatherback
Beware: Crazy Scientist
 
leatherback's Avatar
 
Join Date: Mar 2002
Location: Oxford (UK work), Melbourne (AU work), Vreden (DE partner), Zutphen (nl parents)
Posts: 5,183
Quote:
Originally Posted by zeb
mrhappiness: I just re-read my last post and realized that my tone was slightly harsh. That wasn't my intent. It was still early for me...
/me bows and lifts hat:
I wondered about that post.. Takes a big man to get back to a shortened out fuse

I agree with the scalability problems & risks. I am not sure whether you can get it to work though. I have found some hickups in variable treatments in PhP. But maybe.. Someone comes with a good solution.

Good luck

J.
__________________
- Codes provide logic/technique, not full working solutions
- Use: or die(mysql_error()) after each query and safe a headache
- use print_r($variable) to see the values in an array
------------------------
latest project|me|Coding tips
syntax editor|Get your question answered
leatherback is offline   Reply With Quote
Old 08-05-2005, 01:00 PM   #9
laserlight
PHP Witch
 
laserlight's Avatar
 
Join Date: Apr 2003
Location: Singapore
Posts: 12,543
Quote:
It does not differentiate between "never been set" and "set but NULL"
That's because NULL also means "never been set". Read the PHP Manual on NULL for more information.
__________________
Use Bazaar for your version control system
Read the PHP Spellbook
Learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Old 08-05-2005, 01:15 PM   #10
zeb
Senior Member
 
Join Date: Jun 2003
Location: Maine
Posts: 119
Thanks leatherback.
__________________
..:: Zeb ::..

zebellis.com
zeb is offline   Reply With Quote
Old 08-05-2005, 01:40 PM   #11
zeb
Senior Member
 
Join Date: Jun 2003
Location: Maine
Posts: 119
Quote:
Originally Posted by laserlight
That's because NULL also means "never been set". Read the PHP Manual on NULL for more information.
Not exactly, they say
Quote:
A variable is considered to be NULL if
  • it has been assigned the constant NULL.
  • it has not been set to any value yet.
  • it has been unset().
they use the phrase "considered to be NULL if". NULL can't mean "never been set" if you can set it to NULL in the first place. It has been set. It's a contradiction. Consider too the fact that you can pull a NULL value from a database. from the manual page for mysql_fetch_row(): "Note: This function sets NULL fields to PHP NULL value." The resulting array contains an index that does exist and has been set to NULL. That's different from that index "never being set". That index now exists to inform you that there is a value in the database that is NULL. So the fact that it exists is important which necessitates the need to differentiate between an existing variable/index and one that in fact isn't there.
__________________
..:: Zeb ::..

zebellis.com
zeb is offline   Reply With Quote
Old 08-05-2005, 01:48 PM   #12
zeb
Senior Member
 
Join Date: Jun 2003
Location: Maine
Posts: 119
I'm not saying that never-been-set variables and indices shouldn't be considered NULL. In most circumstances, it's enough to test a variable existence with isset or is_null. I am saying that there are times when it's helpful to know if a variable/index exists (has been executed and bound to something) versus simply being set to NULL.
__________________
..:: Zeb ::..

zebellis.com
zeb is offline   Reply With Quote
Old 08-05-2005, 01:56 PM   #13
leatherback
Beware: Crazy Scientist
 
leatherback's Avatar
 
Join Date: Mar 2002
Location: Oxford (UK work), Melbourne (AU work), Vreden (DE partner), Zutphen (nl parents)
Posts: 5,183
Hi Zeb,

As I posted earlier.. PhP is not as strickt as most languages. So.. In most languages you can initiate a var, by saying: varname = vartype. In PhP thsi distinction is not made. So.. NULL == unset() = not set yet
__________________
- Codes provide logic/technique, not full working solutions
- Use: or die(mysql_error()) after each query and safe a headache
- use print_r($variable) to see the values in an array
------------------------
latest project|me|Coding tips
syntax editor|Get your question answered
leatherback is offline   Reply With Quote
Old 08-05-2005, 01:58 PM   #14
laserlight
PHP Witch
 
laserlight's Avatar
 
Join Date: Apr 2003
Location: Singapore
Posts: 12,543
Quote:
they use the phrase "considered to be NULL if". NULL can't mean "never been set" if you can set it to NULL in the first place. It has been set. It's a contradiction.
I disagree. Here the concept of NULL means "no value". If a variable is not set, or has been unset, it has no value. Likewise, if a variable is set to NULL, it has no value.

Quote:
The resulting array contains an index that does exist and has been set to NULL. That's different from that index "never being set". That index now exists to inform you that there is a value in the database that is NULL.
This fits in with a concept of null values in a database. The field has no (or an unknown) value, but it exists. You can check for the presence of the field (as represented by the array element) with array_key_exists().

EDIT:
You did that in your example, and as far as I can tell, it should be the way to go for what you want to do.
__________________
Use Bazaar for your version control system
Read the PHP Spellbook
Learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Old 08-05-2005, 02:16 PM   #15
zeb
Senior Member
 
Join Date: Jun 2003
Location: Maine
Posts: 119
I'm not really talking about "no value", but that's the problem with PHP, it groups all these cases:
• variable has no value, but exists
• variable use to have values, but no longer has one
• variable? what variable? it never existed
• variable use to exist, but has been released from existence (unset)
into one type without giving you the way to distinguish between them.

Quote:
Originally Posted by zeb
They're the same except for the fact that a variable or index can be non-existent before it can be set to NULL. Those are two different states. They both test out the same conditionally in PHP, but the differences in those two states is undeniable.
Is this not undeniable?
__________________
..:: Zeb ::..

zebellis.com
zeb is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT -4. The time now is 01:53 PM.






Acceptable Use Policy


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.