The post by <rburghol a t veetee dot edu> has nothing to do with the special NULL value. He's comparing a float (0.0) to a string ('NULL'). The special NULL value is correctly represented as a bareword (NULL), not a string ('NULL').
<?php
/* start <rburghol a t veetee dot edu> example, with comment */
$tvar = 0.0;
// The string 'NULL' is converted to integer 0 for the comparison
if ($tvar == 'NULL') {
print("Match");
} else {
print("No Match");
}
/* end <rburghol a t veetee dot edu> example */
// Example using actual NULL
if ($tvar == NULL) {
print("Match");
} else {
print("No Match");
}
?>
Output:
Match
Match
It's the same result, but it's important to know what's happening and why, and how to represent NULL.
NULL
Специалната стойност NULL обозначава, че променливата няма стойност. NULL е единствената възможна стойност от тип NULL.
Забележка: Типът null беше въведен в PHP 4.
Променливата се счита за NULL ако
-
й е била присвоена константата NULL.
-
все още не е установена в никаква стойност.
-
е била изчистена с unset().
NULL
php_manual at SPAMNONE dot jessemccarthy dot net
01-Jun-2008 07:12
01-Jun-2008 07:12
rburghol a t veetee dot edu
06-Mar-2008 05:17
06-Mar-2008 05:17
With php 5.2.4 on Windows, a variable value of 0 is evaluated as equivalent to the string 'NULL'. So, one must use the strict equivalency test here too (===).
Example:
<?php
$tvar = 0.0;
if ($tvar == 'NULL') {
print("Match");
} else {
print("No Match");
}
if ($tvar === 'NULL') {
print("Match");
} else {
print("No Match");
}
?>
Output:
Match
No Match
alexey at gmail dot com
27-Nov-2007 08:26
27-Nov-2007 08:26
jaumesb aat consert doot net mentions that certain values such as empty strings or 0 convert to NULL if compared using ==. However, objects also evaluate to NULL if they have no variable members:
<?php
class js_incomplete_object {
function js_incomplete_object() {
}
}
$tmp=new js_incomplete_object();
if ($tmp==null) {
echo "null\n";
} else {
echo "not NULL\n";
}
?>
The answer is NULL. However, if you add $this->a=1; to the constructor then the answer is not NULL.
james
20-Sep-2007 05:25
20-Sep-2007 05:25
A little speed test:
<?php
$v = NULL;
$s = microtime(TRUE);
for($i=0; $i<1000; $i++) {
is_null($v);
}
print microtime(TRUE)-$s;
print "<br>";
$s = microtime(TRUE);
for($i=0; $i<1000; $i++) {
$v===NULL;
}
print microtime(TRUE)-$s;
?>
Results:
0.017982006072998
0.0005950927734375
Using "===" is 30x quicker than is_null().
nl-x at bita dot nl
09-Jul-2007 07:33
09-Jul-2007 07:33
Watch out. You can define a new constant with the name NULL with define("NULL","FOO");. But you must use the function constant("NULL"); to get it's value. NULL without the function call to the constant() function will still retrieve the special type NULL value.
Within a class there is no problem, as const NULL="Foo"; will be accessible as myClass::NULL.
cdcchen at hotmail dot com
26-May-2006 05:17
26-May-2006 05:17
empty() is_null() !isset()
$var = "";
empty($var) is true.
is_null($var) is false.
!isset($var) is false.
28-Apr-2006 12:16
To zola at zolaweb dot com, if you are trying to default fields to NULL when empty you should just pass the empty string in the SQL statement and have the column in the table set up with a default value of NULL. Makes things a little less confusing. :-)
owk dot ch199_ph at gadz dot org
07-Mar-2006 07:37
07-Mar-2006 07:37
To zola at zolaweb dot com :
"It treats NULL as the constant and unsets the variable."
It doesn't. As you are trying to find if your variable is empty, your code should be :
if (empty ($array['var'])) {
$array['var'] = "NULL";
}
You know, when "!isset ($var)" is true, $var is already not set...
PS : when "$var = '';", $var is set AND is equal to '', the empty string.
zola at zolaweb dot com
15-Jan-2006 06:53
15-Jan-2006 06:53
Discovered something probably worth mentioning.
I had a form that had several values that didn't have to be set. I wanted to use the word "NULL" as a word (as opposed to the NULL constant) to go into the SQL statement when creating a new record.
If I do a check based on the variable having no value:
if ($array['var'] == "")
{
$array['var'] = "NULL";
}
Then $array['var'] contains the word "NULL" the way I want it to, BUT I have to be careful with the sql statement (more on this in a moment)
On the other hand, if I check via !isset()
if (!isset($array['var']))
{
$array['var'] = "NULL";
}
It treats NULL as the constant and unsets the variable.
In the SQL, if I am inserting and put it in as is:
$sql = "INSERT INTO mytable VALUES(NULL, ".$array['var'] .", ' ".$array['some_other_var'] ." ')";
the word NULL replaces $array['var'] as it should, but if I enclose the variable in single quotes (because maybe that variable, if it's set, will contain a space)
$sql = "INSERT INTO mytable VALUES(NULL, ' ".$array['var'] ." ', ' ".$array['some_other_var'] ." ')";
again, it treats NULL as the constant NULL instead of the word.
This seems inconsistent--one would have thought that enclosing it in double quotes would say I want the letters NULL as opposed to the constant, and I'll bug report it as well but wanted to mention it here for other users.
06-Jan-2006 10:51
// Difference between "unset($a);" and "$a = NULL;" :
<?php
// unset($a)
$a = 5;
$b = & $a;
unset($a);
print "b $b "; // b 5
// $a = NULL; (better I think)
$a = 5;
$b = & $a;
$a = NULL;
print "b $b "; // b
print(! isset($b)); // 1
?>
getphp at gmx dot net
04-Aug-2005 01:54
04-Aug-2005 01:54
in addition to poutri_j at epitech dot net:
you missed that ($var == null) dont respect the type ... try this:
<?php
$t = array();
if ($t === NULL OR is_null($t)) {
echo "Value is NULL";
} elseif ($t === TRUE) {
echo "Value is TRUE";
} elseif ($t == NULL OR empty($t)) {
echo "Value not set or empty.";
}
?>
Output:
Value maybe not set or empty.
PS: Yes, some conditions are redundant. This was intended.
poutri_j at epitech dot net
26-Jul-2005 01:56
26-Jul-2005 01:56
if you declare something like this :
class toto
{
public $a = array();
public function load()
{
if ($this->a == null) // ==> the result is true
$a = other_func();
}
}
be carefull, that's strange but an empty array is considered as a null variable
disappear at dissolution dot ath dot cx
02-May-2005 12:20
02-May-2005 12:20
Hi,
Im using PHP 5.0.3
i wrote a small null study to test the cases here and this is the results i got
Code ::
<?php
$Array = array ( 0 , '' , FALSE , NULL ) ;
$ArrayCount = count ( $Array ) ;
$String .= '$Array = ' . "array ( 0 , '' , FALSE , NULL ) <br><br>" ;
for ( $i = 0 ; $i < $ArrayCount ; $i++ )
{
if ( $Array [ $i ] == NULL )
{
$String .= '$Array [ $i ] == NULL :: $Array [ ' . $i . ' ] <br>' ;
}
if ( $Array [ $i ] === NULL )
{
$String .= '$Array [ $i ] === NULL :: $Array [ ' . $i . ' ] <br>' ;
}
if ( is_null ( $Array [ $i ] ) )
{
$String .= 'is_null ( $Array [ $i ] ) :: $Array [ ' . $i . ' ] <br>' ;
}
}
echo $String ;
?>
Results ::
$Array = array ( 0 , '' , FALSE , NULL )
$Array [ $i ] == NULL :: $Array [ 0 ]
$Array [ $i ] == NULL :: $Array [ 1 ]
$Array [ $i ] == NULL :: $Array [ 2 ]
$Array [ $i ] == NULL :: $Array [ 3 ]
$Array [ $i ] === NULL :: $Array [ 3 ]
is_null ( $Array [ $i ] ) :: $Array [ 3 ]
jaumesb aat consert doot net
26-Feb-2005 09:38
26-Feb-2005 09:38
Note that the expression
( $v == NULL )
will evaluate as TRUE if $v is zero or the empty string.
To avoid this, remember to use :
( $v === NULL )
rizwan_nawaz786 at hotmail dot com
19-Oct-2004 06:22
19-Oct-2004 06:22
Hi
Rizwan Here
Null is the Constant in PHP. it is use to assign a empty value to the variable like
$a=NULL;
At this time $a has is NULL or $a has no value;
When we declaire a veriable in other languages than that veriable has some value depending on the value of memory location at which it is pointed but in php when we declaire a veriable than php assign a NULL to a veriable.
pozmu at wp dot pl
21-Apr-2004 05:21
21-Apr-2004 05:21
responding to joemamacow at hotmail dot com comment:
if you are using isset() to check is the variable set, the more logically and clear way to "delete" the variable is to use unset() (http://www.php.net/unset ) function:
<?php
unset($variable);
?>
Of course setiing variable value to NULL is also OK.
alex at netflex dot nl
13-Jun-2002 01:13
13-Jun-2002 01:13
Hi,
Function for looking if it is a NULL
<?php
$var = NULL;
if (isnull("var")) {
echo "var===NULL\n";
} else {
echo "var!==NULL\n";
}
if (isnull("test")) { // give FALSE, test is not set
echo "test===NULL\n";
} else {
echo "test!==NULL\n";
}
$array['var'] = NULL;
if (isnull("var", $array)) {
echo "array['var']===NULL\n";
} else {
echo "array['var']!==NULL\n";
}
function isnull($var, $base = FALSE) {
if ($base===FALSE) {
$base = &$GLOBALS;
} elseif (!is_array($base)) {
return FALSE;
}
if ((array_key_exists($var, $base))&&($base[$var]===NULL)) {
return TRUE;
} else {
return FALSE;
}
}
?>
avbentem at hetnet.nl
22-Feb-2002 10:25
22-Feb-2002 10:25
To extend a bit on tbdavis's comment:
:: NULL == NULL is true
:: NULL == FALSE is true
:: NULL == TRUE is false
However: note the implicit type conversions that PHP performs! When using 'identical' instead of 'equal' then both NULL === FALSE and NULL === TRUE yield FALSE. An overview is easily created using something like
function evalExpr( $desc )
{
echo str_pad($desc , 15) . "--> ";
var_dump( eval( "return(" . $desc . ");" ));
}
Note that even TRUE AND TRUE does not evaluate to a boolean value, and that OR and XOR behave different as well!
PHP Version: 4.0.6
false --> bool(false)
true --> bool(true)
null --> NULL
!null --> bool(true)
true and true --> int(1)
true and false --> int(0)
true or true --> int(1)
true or false --> int(1)
true xor true --> bool(false)
true xor false --> bool(true)
true == null --> bool(false)
true === null --> bool(false)
true != null --> bool(true)
true !== null --> bool(true)
false == null --> bool(true)
false === null --> bool(false)
false != null --> bool(false)
false !== null --> bool(true)
null == null --> bool(true)
null != null --> bool(false)
null === null --> bool(true)
null !== null --> bool(false)
null or null --> int(0)
null xor null --> bool(false)
null and null --> int(0)
true or null --> int(1)
true xor null --> bool(true)
true and null --> int(0)
false or null --> int(0)
false xor null --> bool(false)
false and null --> int(0)
true < null --> bool(false)
true > null --> bool(true)
false < null --> bool(false)
false > null --> bool(false)
1 + null --> int(1)
"text" . null --> string(4) "text"
Finally, for those who do not know SQL: in SQL the NULL value is evaluated a bit like "I do not know; it could be anything, like 0, 1, a, b, true, false or even nothing at all". This implies that in SQL NULL == NULL could be interpreted as "could be anything" == "could be something else", which does not yield true! Instead, it yields NULL, which boils down to FALSE in boolean context...
Likewise, in SQL:
NULL AND TRUE yields NULL
NULL OR TRUE yields TRUE
NULL AND FALSE yields FALSE
NULL OR FALSE yields NULL
NULL == TRUE yields FALSE
NULL == FALSE yields FALSE
a.
sc at dbtech dot de
17-Feb-2002 12:59
17-Feb-2002 12:59
NULL as the best way to detect additional parameters of unknown type:
function FooBar($Param = NULL) {
if (is_null($Param)) {
[...]
junk dot phpnet at gasolinemm dot com
18-Nov-2001 06:19
18-Nov-2001 06:19
$a = "";
$b = NULL;
$a == $b;
/* returns true: $a has been converted to $b for the equality comparison */
is_null($a); //returns false
is_null($b); //returns true
$a === $b;
/* returns false: $a is not _identical_ to $b */
dward at maidencreek dot com
13-Nov-2001 12:52
13-Nov-2001 12:52
Nulls are almost the same as unset variables and it is hard to tell the difference without creating errors from the interpreter:
$var = NULL;
isset($var) is FALSE
empty($var) is TRUE
is_null($var) is TRUE
isset($novar) is FALSE
empty($novar) is TRUE
is_null($novar) gives an Undefined variable error
$var IS in the symbol table (from get_defined_vars())
$var CAN be used as an argument or an expression.
So, in most cases I found that we needed to use !isset($var) intead of is_null($var) and then set $var = NULL if the variable needs to be used later to guarantee that $var is a valid variable with a NULL value instead of being undefined.
tbdavis at greyshirt dot net
11-Oct-2001 01:36
11-Oct-2001 01:36
Unlike the relational model, NULL in PHP has the following properties:
NULL == NULL is true,
NULL == FALSE is true.
And in line with the relational model, NULL == TRUE fails.
