2

Possible Duplicate:
Can you unset() many variables at once in PHP?

$var1 = $var2 = $tvar3 = null;

Is it okay to remove variables like this?

Are there better ways to unset couple of variables?

Community
  • 1
  • 1
James
  • 42,081
  • 53
  • 136
  • 161

1 Answers1

12

unset() is variadic (i.e. it takes any number of arguments and unsets them all):

unset($var1, $var2, $var3);

Also note that unset() is not the same as setting to NULL. Using unset() you actually remove the variables, whereas setting them to NULL keeps them defined, just with a "value" of NULL. Doing it either way causes isset() to return false on those variables, but they're still semantically and technically different.

Here's a quick proof:

<?php

$x = NULL;
var_dump(array_key_exists('x', $GLOBALS)); // bool(true)

unset($x);
var_dump(array_key_exists('x', $GLOBALS)); // bool(false)

?>
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
  • 1
    For the record, if anyone wants me to delete my answer just say so in a comment instead of downvoting or revoking upvotes. And even then I'll probably refuse because only this question has the `= NULL` vs `unset()` conundrum which I've taken the liberty to explain here even before the duplicate was found. Unless, of course, someone finds an older question asking about it. – BoltClock Feb 05 '11 at 13:27