49

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

I have 3 variables var1 var2 var3. Is there's a way of un-setting them without repeated use of unset() function ?

Community
  • 1
  • 1
Yousuf Memon
  • 4,638
  • 12
  • 41
  • 57

3 Answers3

178

try this

unset($foo1, $foo2, $foo3);
webCoder
  • 2,192
  • 1
  • 16
  • 29
5

Don't use foreach loop for this. Since it works with a copy of array.

See Example

http://codepad.org/mZOc81J5

IF you want to do this using loop then use for loop.

Community
  • 1
  • 1
Ashwini Agarwal
  • 4,828
  • 2
  • 42
  • 59
3

use like this

for($i=0 ; $i<count($array) ; $i++)
{
    unset($array[$i]);
}

You have to use for loop for this.

you can use foreach loop but it will not unset all variable one variable still remains.

foreach($array as $arr)
{
    unset($array[$arr]);
}
Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100