014php_ Null - Manual

download 014php_ Null - Manual

of 5

Transcript of 014php_ Null - Manual

  • 7/30/2019 014php_ Null - Manual

    1/5

    [edit] Last updated: Fri, 20 Apr 2012

    NULL

    The special NULL value represents a variable with no value. NULL is the only possible value of type NULL.

    A variable is considered to be null if:

    it has been assigned the constant NULL.

    it has not been set to any value yet.

    it has been unset().

    Syntax

    There is only one value of type null, and that is the case-insensitive constant NULL.

    See also the functions is_null() and unset().

    Casting to NULL

    Casting a variable to null using (unset) $var will not remove the variable or unset its value. It will only return a NULL value.

    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:

  • 7/30/2019 014php_ Null - Manual

    2/5

    1 of 5 26/04/2012 11:41

    $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.

    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

  • 7/30/2019 014php_ Null - Manual

    3/5

    $s = microtime(TRUE);

    for($i=0; $i

    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;" :

  • 7/30/2019 014php_ Null - Manual

    4/5

    l h // h / l/ /l ll h

  • 7/30/2019 014php_ Null - Manual

    5/5

    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:

    isset($var) is FALSEempty($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.

    Copyright 2001-2012 The PHP GroupAll rights reserved.

    PHP: NULL - Manual http://www.php.net/manual/en/language.types.null.php