downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

本ドキュメントにおける疑似的な型および変数> <リソース
[edit] Last updated: Fri, 10 Feb 2012

view this page in

NULL

特別な NULL 値は、ある変数が値を持たないことを表します。NULL は、NULL 型の唯一の値です。

変数は、以下の場合に NULL とみなされます。

  • 定数 NULL が代入されている場合。

  • まだ値が何も代入されていない場合。

  • unset() されている場合。

構文

NULL 型の値は一つだけで、 大文字小文字を区別しない定数 NULLです。

<?php
$var 
NULL;       
?>

is_null() および unset() も参照ください。

NULL へのキャスト

(unset) $var を使って変数を null にキャストすると、 その変数を削除したり値の設定を解除したりはしません。 単に NULL 値を返すだけです。



add a note add a note User Contributed Notes NULL
ryan at trezshard dot com 01-Jun-2011 08:31
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.
quickpick 22-Apr-2011 03:36
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
james 20-Sep-2007 08: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 10: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 25-May-2006 08:17
empty() is_null() !isset()

$var = "";

empty($var) is true.
is_null($var) is false.
!isset($var) is false.
06-Jan-2006 01: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
?>
poutri_j at epitech dot net 26-Jul-2005 04:56
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
rizwan_nawaz786 at hotmail dot com 18-Oct-2004 09: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.
dward at maidencreek dot com 12-Nov-2001 03:52
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.
tbdavis at greyshirt dot net 11-Oct-2001 04: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.

 
show source | credits | stats | sitemap | contact | advertising | mirror sites