Page 1 of 1

Use NULL or FALSE?

Posted: Tue Sep 25, 2007 9:23 am
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?

Posted: Tue Sep 25, 2007 10:14 am
by feyd
Either is acceptable as long as you're consistent.

Personally, I prefer NULL. You can use === for checking it.

Posted: Tue Sep 25, 2007 11:59 am
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().

Posted: Tue Sep 25, 2007 12:07 pm
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().

Posted: Tue Sep 25, 2007 12:08 pm
by feyd
Null.

Posted: Tue Sep 25, 2007 12:46 pm
by PhpMachine
Should I use is_resource() instead of ==TRUE or !is_null() (which doesn't work) ?

Posted: Tue Sep 25, 2007 1:00 pm
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...

Posted: Tue Sep 25, 2007 1:17 pm
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?

Posted: Tue Sep 25, 2007 1:47 pm
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.

Posted: Tue Sep 25, 2007 2:01 pm
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 ;)

Posted: Tue Sep 25, 2007 2:05 pm
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...

Posted: Tue Sep 25, 2007 2:07 pm
by volka
PhpMachine wrote:So then you should check for both the empty string and FALSE, to be sure?
no, just FALSE.

Posted: Tue Sep 25, 2007 2:09 pm
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?

Posted: Tue Sep 25, 2007 2:22 pm
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.