Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unsetting a variable vs setting to ''

Is it better form to do one of the following? If not, is one of them faster than the other?

unset($variable);

or to do

$variable = '';
like image 614
waiwai933 Avatar asked Jan 11 '10 04:01

waiwai933


2 Answers

they will do slightly different things:

  • unset will remove the variable from the symbol table and will decrement the reference count on the contents by 1. references to the variable after that will trigger a notice ("undefined variable"). (note, an object can override the default unset behavior on its properties by implementing __unset()).

  • setting to an empty string will decrement the reference count on the contents by 1, set the contents to a 0-length string, but the symbol will still remain in the symbol table, and you can still reference the variable. (note, an object can override the default assignment behavior on its properties by implementing __set()).

in older php's, when the ref count falls to 0, the destructor is called and the memory is freed immediately. in newer versions (>= 5.3), php uses a buffered scheme that has better handling for cyclical references (http://www.php.net/manual/en/features.gc.collecting-cycles.php), so the memory could possibly be freed later, tho it might not be delayed at all... in any case, that doesn't really cause any issues and the new algorithm prevents certain memory leaks.

if the variable name won't be used again, unset should be a few cpu cycles faster (since new contents don't need to be created). but if the variable name is re-used, php would have to create a new variable and symbol table entry, so it could be slower! the diff would be a negligible difference in most situations.

if you want to mark the variable as invalid for later checking, you could set it to false or null. that would be better than testing with isset() because a typo in the variable name would return false without any error... you can also pass false and null values to another function and retain the sentinel value, which can't be done with an unset var...

so i would say:

$var = false; ...
if ($var !== false) ...

or

$var = null; ...
if (!is_null($var)) ...

would be better for checking sentinel values than

unset($var); ...
if (isset($var)) ...
like image 62
jspcal Avatar answered Sep 19 '22 16:09

jspcal


Technically $test = '' will return true to

if(isset($test))

Because it is still 'set', it is just set to en empty value.

It will however return true to

if(empty($test))

as it is an empty variable. It just depends on what you are checking for. Generally people tend to check if a variable isset, rather than if it is empty though.

So it is better to just unset it completely.

Also, this is easier to understand

unset($test);

than this

$test = '';

the first immediately tells you that the variable is NO LONGER SET. Where as the latter simply tells you it is set to a blank space. This is commonly used when you are going to add stuff to a variable and don't want PHP erroring on you.

like image 34
Tyler Carter Avatar answered Sep 22 '22 16:09

Tyler Carter