0

As we all know if a variable is created without a value, it is automatically assigned a value of NULL.

I have following code snippets :

<?php
    $name;
    echo $name;     
?>

AND

<?php
    $name;
    print $name;     
?>

Both of the above code snippets' output is as below(it's exactly the same) :

Notice: Undefined variable: name in C:\xampp\htdocs\php_playground\demo.php on line 7

I have another code snippet :

<?php
    $name;
    var_dump($name);     
?>

The output of above(last) code snippet is as below :

Notice: Undefined variable: name in C:\xampp\htdocs\php_playground\demo.php on line 8
NULL

So, my question is why the value "NULL" is not getting displayed when I tried to show it using echo and print?

However, the "NULL" value gets displayed when I tried to show it using var_dump() function.

Why this is happening?

What's behind this behavior?

Thank You.

PHPLover
  • 1
  • 51
  • 158
  • 311
  • 1
    Because both `echo` and `print` implicitly cast a value to a string. And `(string)NULL` is an empty string. So it's there you just cannot see it. And `var_dump` according to its purpose does not cast anything but dumps as-is. – zerkms Dec 13 '16 at 03:48
  • @zerkms : so whatever I see as output of var_dump() function is string "NULL" on a screen. This string is a data-type or a value of an empty variable? – PHPLover Dec 13 '16 at 04:02
  • It's a value `NULL` of type `NULL`. http://php.net/manual/en/language.types.php http://php.net/manual/en/language.types.null.php – zerkms Dec 13 '16 at 04:09
  • The notice is **not** an output of `echo` or `print`. It is a *notice* generated by the PHP parser, warning you that you attempt to work on an undefined variable. On a productive system you would configure PHP not to show notices, warnings and errors, but to log them into a file. `var_dump` is for code inspection in development / debugging time, giving you more technical information. – Pinke Helga Dec 13 '16 at 04:13
  • The problem is: PHP as a script language does not well distinguish between *NULL* and *undefined* in some situations as languages compiling into native code would do. – Pinke Helga Dec 13 '16 at 04:21

1 Answers1

4

The problem you're having is that NULL isn't anything - it's the absence of a value.

When you try to echo or print it, you get the notice about an Undefined Variable because the value of $name is not set to anything, and you can't echo the absence of something.

$name;
var_dump($name);

The output of this will be NULL to tell you that the variable had no value. It's not a string with the value of "NULL", it's just NULL, nothing, the absence of something.

Compare this to the following:

$name = '';
var_dump($name);

This outputs string(0)"" - this is telling you that $name DID have a value, which was a string which contained no characters ("") totalling a length of 0.

Finally, look at the following:

$name = 'test';
var_dump($name);

This outputs string(4)"test" - a string containing test, which had a length of 4

Number 6
  • 56
  • 3
  • Are you saying that output of is as a data-type "NULL" and not as a value "NULL"? – PHPLover Dec 13 '16 at 05:37
  • Almost - NULL is neither a value or a datatype, but a constant representing the lack of a value. It's a bit of a special case. – Number 6 Dec 13 '16 at 06:24
  • 1
    "it's the absence of a value." --- this is misleading. `$a = null;` <- in this statement I explicitly assigned a particular value to the variable. – zerkms Dec 14 '16 at 20:57
  • @Number6: If you have answer to the above comment by zerkms please reply to it. I would also like to know your reply to it. Thank. – PHPLover Sep 01 '17 at 08:21
  • 1
    It's really a matter of semantics. NULL can be _assigned_ to a variable yes, and in that sense it _looks_ like a value, however it's a special case and is not a value in same sense that the integer 5 or the string "test" are. It _represents_ the lack of an _actual_ value; _actual_ values represent particular data (such as a string, an integer, etc.) held in memory but NULL is different because it's a conceptualisation of "nothing at all". – Number 6 Sep 02 '17 at 15:13