Avinash Dubey Avinash Dubey - 1 month ago 6
PHP Question

What is difference between assigning NULL and unset?

take a look in following code:

<?php
$a = 20;
$a= NULL; //or unset($a)
if(isset($a))
{
print("hi");
}
else
{
echo "not initiated";
}
if(isset($b)) //$b is a variable which is not initialized
{
print("hi");
}
else
{
echo "not initiated";
}

?>


and when apply unset, i get the same result:

So, What is difference between assigning NULL and unset?

Answer

As you can see below, both will behave pretty much the same for standard comparison operations.

Use unset() to free large objects / arrays that aren't used anymore but cannot be freed by the GC because of references still being held elsewhere.

-------------------------------------------------------------------------------
|   Expression     | gettype()  | empty()  | is_null()  | isset()  | boolean  |
-------------------------------------------------------------------------------
| $x = "";         | string     | TRUE     | FALSE      | TRUE     | FALSE    |
| $x = null        | NULL       | TRUE     | TRUE       | FALSE    | FALSE    |
| var $x;          | NULL       | TRUE     | TRUE       | FALSE    | FALSE    |
| $x is undefined  | NULL       | TRUE     | TRUE       | FALSE    | FALSE    |
-------------------------------------------------------------------------------