phpstringvariablesnullunset

PHP several ways to reset a variable


I know a few methods to reset a variable in PHP.

Problem is that I don't know exactly what is the difference between them and who is faster so here I'm asking...

What is the difference between :

<?php

$resetME = null;
//VS    
unset($resetME);
//VS
$resetME = 0;

?>

and...

I would be happy to know if there is other tricks to completely remove a variable from memory.

Moreover, I would LOVE to know if there is any reason to reset a variable on the end of the page since server reset them anyways when page fully loaded...

Just to clarify more...

<?php

$a = 1;
echo $a;

//is there any reason to unset $a on the end of the page ?
unset($a);

?>

Thanks in advance !


Solution

  • $resetME = null; // Will not erase var from the memory
      
    unset($resetME); // Will erase var from the memory
    
    $resetME = 0; // Will not erase var from the memory
    

    To completely remove a variable from memory you need to use unset. All other ways just change variable value. But if variable doesn't exists, you'll receive an error by using unset($resetME); in which case you might use $resetME = null; or if (isset($resetME)) {unset($resetME)}

    Anyways, after script ends, PHP will clean memory.