0

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?

hakre
  • 193,403
  • 52
  • 435
  • 836
Avinash Dubey
  • 128
  • 1
  • 3
  • 12
  • [This comment](http://www.php.net/manual/en/function.unset.php#105980) from the `unset()` documentation make some comparisons. – Lix Oct 12 '12 at 12:56
  • possible duplicate of [What's better at freeing memory with PHP: unset() or $var = null](http://stackoverflow.com/questions/584960/whats-better-at-freeing-memory-with-php-unset-or-var-null) – Thirumalai murugan Jul 01 '13 at 07:19

7 Answers7

7

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    |
-------------------------------------------------------------------------------
aefxx
  • 24,835
  • 6
  • 45
  • 55
4

as someone states from unset()

unset() does just what it's name says - unset a variable. It does not force immediate memory freeing. PHP's garbage collector will do it when it see fits - by intention as soon, as those CPU cycles aren't needed anyway, or as late as before the script would run out of memory, whatever occurs first.

If you are doing $whatever = null; then you are rewriting variable's data. You might get memory freed / shrunk faster, but it may steal CPU cycles from the code that truly needs them sooner, resulting in a longer overall execution time.

Community
  • 1
  • 1
SubjectCurio
  • 4,702
  • 3
  • 32
  • 46
2

FORM PHP DOC isset

Determine if a variable is set and is not NULL.

If a variable has been unset with unset(), it will no longer be set. isset() will return FALSE if testing a variable that has been set to NULL. Also note that a NULL byte ("\0") is not equivalent to the PHP NULL constant.

On unset

unset() destroys the specified variables.

It does not matter if its initiated or not since you are using isset it would return false for both initiated variable and null values

Community
  • 1
  • 1
Baba
  • 94,024
  • 28
  • 166
  • 217
2

You are using isset(). According to the manual, isset() checks if a variable is set and not NULL. So you cannot use isset() to distinguish between a variable that is not set and a variable that is NULL.

Setting a variable to NULL means exactly that: The variable is set to a value and that value is the special value NULL.

On the other hand unset() means the variable is no longer set. It doesn't have a value and basically doesn't exist anymore.

This has implications for garbage collection and other things, if you want to dive deeper.

Trott
  • 66,479
  • 23
  • 173
  • 212
1

Everything has been pretty much already answered, but I just wanted to add this example:

$a = 1;
$b = 2;

if ( $a == true ) echo 'Value'; else echo 'No value';
if ( $b == true ) echo 'Value'; else echo 'No value';

/*
Value
Value
*/

$a = NULL;
unset( $b );

if ( $a == true ) echo 'Value'; else echo 'No value';
if ( $b == true ) echo 'Value'; else echo 'No value';

/*
No value

Notice: Undefined variable: b in C:\test\test.php on line 13
No value
*/

The results might be close, but the last one produces a notice, since there is no such variable anymore.

Peon
  • 7,902
  • 7
  • 59
  • 100
0

The main difference is that setting a variable to NULL like:

$a = NULL;

is assigning a value, here the NULL value.

Albeit unset() like

unset($a);

is un-setting the variable, that means removing it from the variable table.

As undefined variables are all NULL in PHP you might not see the difference.

However if the variable you unset is an alias, only the alias is removed, there is no assignment of the value NULL to it.

A demonstration of that:

<?php                         // 01
                              // 02
$a = 1;                       // 03
$b = &$a;                     // 04
                              // 05
$b = NULL;                    // 06
                              // 07
var_dump($a);                 // 08
                              // 09
unset($b);                    // 10
                              // 11
var_dump($a);                 // 12
                              // 13    
unset($a);                    // 14
                              // 15
var_dump($a);                 // 16

Output:

NULL
NULL

Notice: Undefined variable: a in /t.php on line 16
NULL

http://codepad.org/cC1vRx0W

hakre
  • 193,403
  • 52
  • 435
  • 836
0

Setting a variable to NULL means assigns the value NULL to the variable exactly as the variable is set to a value and value is the special value NULL.

Whereas unset() destroys the specified variables. The behavior of unset() inside of a function can vary depending on what type of variable you are attempting to destroy.

unset() means the variable is no longer set, it doesn’t have a value and basically the variable doesn’t exist anymore.

Mahesh Yadav
  • 2,416
  • 20
  • 23