PHP 5.4.35 Released

NULL

La valeur spéciale NULL représente une variable sans valeur. NULL est la seule valeur possible du type NULL.

Une variable est considérée comme null si :

  • elle s'est vue assigner la constante NULL.

  • elle n'a pas encore reçu de valeur.

  • elle a été effacée avec la fonction unset().

Syntaxe

Il y a une seule valeur de type null, et c'est la constante insensible à la casse NULL.

<?php
$var 
NULL;
?>

Voir aussi les fonctions is_null() et unset().

Transtyper vers NULL

Transtyper une variable vers null en utilisant la syntaxe (unset) $var n'effacera pas la variable, ni écrasera sa valeur. Ca ne fera que retourner la valeur NULL.

add a note add a note

User Contributed Notes 14 notes

up
23
quickpick
3 years ago
Note: empty array is converted to null by non-strict equal '==' comparison. Use is_null() or '===' if there is possible of getting empty array.

$a = array();

$a == null  <== return true
$a === null < == return false
is_null($a) <== return false
up
6
nl-x at bita dot nl
7 years ago
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.
up
1
kuzawinski dot marcin at NOSPAM dot gmail dot com
7 months ago
Funny. It looks like, that there is one, and only one possible value for variable $a that will pass this test:

($a != NULL) && ((bool)$a == NULL)

It's "0" and it works because casting string "0" to boolean gives FALSE (and it's the only non empty string, that works this way). So remember that casting is not "transitive".
up
1
Toycat
1 year ago
Be careful using NULL together with namespaces. If a NULL constant is redefined in a namespace other than global, you will get unexpected results when comparing to NULL inside the namespace. Instead always use \NULL, \FALSE, and \TRUE when comparing. Otherwise it may lead to application failures and potential security issues where certain checks could be effectively disabled.

A simple example to demonstrate the behavior:

<?php
namespace RedefinedConstants {

   
// redefining global namespace constants has no effect
   
define('NULL', 'I am not global NULL!');
   
define('TRUE', 'I am not global TRUE!');
   
define('FALSE', 'I am not global FALSE!');

   
// redefining local namespace constants will work
   
define('RedefinedConstants\NULL', 'I am not NULL!', \TRUE);
   
define('RedefinedConstants\FALSE', 'I am not FALSE!', \TRUE);
   
define('RedefinedConstants\TRUE', 'I am not TRUE!', \TRUE);

   
var_dump(
       
NULL, \NULL, null, \null, Null, \Null,
       
FALSE, \FALSE, false, \false, False, \False,
       
TRUE, \TRUE, true, \true, True, \True
   
);

}
?>
up
1
naitsabes dot pawlak at gmail dot com
2 days ago
Why "" == NULL array() == NULL gives TRUE?

'==' is comparison operator which gives true if both sides are equal after type juggling.

Lets look first at "" == NULL
When you are comparing NULL with string, then NULL is converted to empty string "". So operator '==' is comparing strings in this situation:

"" == NULL

is converted to:

"" == ""

which gives TRUE.

When you are comparing NULL with anything else except strings, then both sides are converted to bool. So when you are comparing null with empty array, then null is converted to FALSE and empty array is converted to FALSE.

array() == NULL

is converted to

FALSE == FALSE

which gives of course TRUE.

I'm basing on http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.types

My previous answer was wrong about comparing "" == NULL. I was assuming that here both sides are also converted to bool, but it isn't true - NULL is converted here to string. You can just check comparison:

"0" == NULL

if in this comparison both sides are converted to bool then it should give TRUE ((bool)NULL = FALSE, (bool)"0" = FALSE). But this comparison gives FALSE.
up
0
naitsabes dot pawlak at gmail dot com
2 days ago
'==' is comparison operator which gives true if both sides are equal after type juggling. When you are comparing bool or null with anything, then both sides are converted to bool.
So when you are comparing null with empty array or empty string, then null is converted to FALSE, empty string is converted to FALSE and empty array is converted to FALSE.

array() == NULL
'' == NULL

both are converted to

FALSE == FALSE

which gives of course TRUE
up
-1
Anonymous
8 months ago
Note the following:
$test ='';

if (isset($test)){
    echo 'Variable test exists';
}
if (empty($test)){ // same result as $test = ''
    echo ' and is_empty';
}
if ($test == null){ // not the same result as is_null($test)
    echo 'and is_null';
}
The result would be:
Variable test exists and is_empty and is_null

But for the following code...:
$test ='';

if (isset($test)){
    echo 'Variable test exists';
}
if (empty($test)){ // same result as $test = ''
    echo ' and is_empty';
}
if ($test === null){  // same result as is_null($test)
    echo 'and is_null';
}
The result would be:
Variable test exists and is_empty

Therefore, for empty string variables, seems that 'empty' and 'null' has the same value but different type.
up
-1
rizwan_nawaz786 at hotmail dot com
10 years ago
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.
up
-2
ryan at trezshard dot com
3 years ago
This simple shorthand seems to work for setting new variables to NULL:

<?php
$Var
;
?>

The above code will set $Var to NULL

UPDATE: After further testing it appears the code only works in the global scope and does not work inside functions.

<?php
function Example(){
 
$Var;
 
var_dump($Var);
}
?>

Would not work as expected.
up
-4
foxdie_cs at hotmail dot com
2 years ago
a quick note about the magic function __get() :

<?php
class Foo{
   
    protected
$bar;
   
    public function
__construct(){
       
       
$this->bar = NULL;
       
var_dump( $this->bar ); //prit 'NULL' but won't call the magic method __get()
       
       
unset( $this->bar );
       
var_dump( $this->bar ); //print 'GET bar' and 'NULL'
           
   
}
   
    public function
__get( $var ){ echo "GET " . $var; }
       
}

new
Foo();
?>
up
-4
poutri_j at epitech dot net
9 years ago
if you declare something like this :

<?php
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
up
-4
dward at maidencreek dot com
13 years ago
Nulls are almost the same as unset variables and it is hard to tell the difference without creating errors from the interpreter:

<?php
$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.
up
-4
cdcchen at hotmail dot com
8 years ago
empty() is_null() !isset()

$var = "";

empty($var) is true.
is_null($var) is false.
!isset($var) is false.
up
-22
Anonymous
8 years ago
// 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
?>
To Top