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
PhpMachine
Forum Commoner
Posts: 42 Joined: Thu Apr 19, 2007 11:26 am
Post
by PhpMachine » Tue Sep 25, 2007 9:23 am
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?
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Tue Sep 25, 2007 10:14 am
Either is acceptable as long as you're consistent.
Personally, I prefer NULL. You can use === for checking it.
Christopher
Site Administrator
Posts: 13596 Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US
Post
by Christopher » Tue Sep 25, 2007 11:59 am
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 » Tue Sep 25, 2007 12:07 pm
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.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Tue Sep 25, 2007 12:08 pm
Null.
PhpMachine
Forum Commoner
Posts: 42 Joined: Thu Apr 19, 2007 11:26 am
Post
by PhpMachine » Tue Sep 25, 2007 12:46 pm
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 » Tue Sep 25, 2007 1:00 pm
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 » Tue Sep 25, 2007 1:17 pm
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:
Which one should I use?
imagecreatefromjpeg() returns a resource object, so I should always use
is_resource() then, right?
Christopher
Site Administrator
Posts: 13596 Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US
Post
by Christopher » Tue Sep 25, 2007 1:47 pm
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)
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Tue Sep 25, 2007 2:01 pm
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 » Tue Sep 25, 2007 2:05 pm
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...
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Tue Sep 25, 2007 2:07 pm
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 » Tue Sep 25, 2007 2:09 pm
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?
Christopher
Site Administrator
Posts: 13596 Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US
Post
by Christopher » Tue Sep 25, 2007 2:22 pm
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)