Use NULL or FALSE?

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
PhpMachine
Forum Commoner
Posts: 42
Joined: Thu Apr 19, 2007 11:26 am

Use NULL or FALSE?

Post by PhpMachine »

Hi all

When should you use NULL or FALSE?

For example, if you initialize your members in a class:

Code: Select all

class A {
 private m_car = [x];

 ...
}

class Car {
 ...
}
Should I set "m_car" to NULL or FALSE?
Which alternative is the most recommended?

Recently, I discovered that "is_null(...)" is not the same as "... == NULL",
where ... is 0 or an empty string.
How come?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Either is acceptable as long as you're consistent.

Personally, I prefer NULL. You can use === for checking it.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

The difference is what they tell a programmer reading the code about the variable. Use true/false to indicate that the variable is a boolean. Use null to indicate that the variable has no value assigned (i.e., will fail isset()). Likewise you would initialize a numeric variable to zero or some starting value; and you would initialize a variable that will be used as an array to array().
(#10850)
PhpMachine
Forum Commoner
Posts: 42
Joined: Thu Apr 19, 2007 11:26 am

Post by PhpMachine »

How would you initialize a member that later is given the "resource" type, using for instance "imagecreatefromjpeg()"?

If I use NULL, then is_null() gives FALSE, no matter what result you get from imagecreatefromjpeg().
Last edited by PhpMachine on Tue Sep 25, 2007 12:47 pm, edited 3 times in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Null.
PhpMachine
Forum Commoner
Posts: 42
Joined: Thu Apr 19, 2007 11:26 am

Post by PhpMachine »

Should I use is_resource() instead of ==TRUE or !is_null() (which doesn't work) ?
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post by jmut »

PhpMachine wrote:Should I use is_resource() instead of ==TRUE or !is_null() (which doesn't work) ?
Well you should know that it is either null or something (resource, string, integer , object of any kind etc)...you would assign the property using setter method so you are 100% sure on this.

That said.... you are as good as

Code: Select all

if (is_null($this->oProperty)) {
    $this->oProperty = new Object();// or setter... method
}

///using $this->oProperty...
PhpMachine
Forum Commoner
Posts: 42
Joined: Thu Apr 19, 2007 11:26 am

Post by PhpMachine »

Hi Jmut.
Using is_null() doesn't work for me.

Code: Select all

$a = NULL;
...
$a = @imagecreatefromjpeg("image.jpg");
Now to check whether $a was successfully initialized, then it works, for instance, with:

Code: Select all

if(!$a)
 if(is_resource($a))
 if(is_object($a))
And it does'nt work with:

Code: Select all

if(!is_null($a))
Which one should I use?

imagecreatefromjpeg() returns a resource object, so I should always use is_resource() then, right?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Please read the manual for direction on how to check specific return values from functions. It spells out what functions return, and in the case of imagecreatefromjpeg() is says:
imagecreatefromjpeg() returns an empty string on failure.
(#10850)
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Code: Select all

<?php
echo phpversion(), "\n";
$a = @imagecreatefromjpeg('a');
var_dump($a);
5.2.4
bool(false)
Anyway, it does not return NULL ;)
PhpMachine
Forum Commoner
Posts: 42
Joined: Thu Apr 19, 2007 11:26 am

Post by PhpMachine »

Hi Christopher
The manual says it returns an empty string, and a bit down under "return values", it says it returns FALSE on error.

So then you should check for both the empty string and FALSE, to be sure?
Sounds strange...
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

PhpMachine wrote:So then you should check for both the empty string and FALSE, to be sure?
no, just FALSE.
PhpMachine
Forum Commoner
Posts: 42
Joined: Thu Apr 19, 2007 11:26 am

Post by PhpMachine »

ok, always a BOOL :)

So, then I should initialize the member with FALSE instead of NULL. :)

Btw, is there a difference between false and FALSE, null AND NULL etc?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

I would initialize it to null to indicate that it is not a string, number or array. I would check with if(!$a) like the docs show.
(#10850)
Post Reply