1

The documentation for NULL says that if I called unset() on a variable, the variable will become 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().

However, this tutorial says the following will happen when calling unset() on a variable:

PHP looks in the symbol table to find the zval corresponding to this variable, decrements the refcount, and removes the variable from the symbol table. Because the refcount is now zero, the garbage collector knows that there is no way of accessing this zval, and can free the memory it occupies.

Now I tried the following code:

<?php
    $x = 12345;
    unset($x);
    echo gettype($x);
?>

The output I got is strange, I got an error that says that the variable is undefined (which conforms with the second quote I have posted), but I also got the type of the variable which is NULL (which conforms with the first quote I have posted):

enter image description here

Why am I getting this strange output?

Steve
  • 691
  • 5
  • 18

2 Answers2

2

unset() destroys the specified variables.

It does not make the Variable NULL so the warning absolutely makes sense

 Notice: Undefined variable: x 

Reference : http://php.net/manual/en/function.unset.php

Thamaraiselvam
  • 6,961
  • 8
  • 45
  • 71
1

unset() destroys the specified variables. Note that in PHP 3, unset() will always return TRUE (actually, the integer value 1). In PHP 4, however, unset() is no longer a true function: it is now a statement. As such no value is returned, and attempting to take the value of unset() results in a parse error

Sinto
  • 3,915
  • 11
  • 36
  • 70