Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jan 2013
    Posts
    74
    Thanks
    19
    Thanked 1 Time in 1 Post

    Clearing a bunch of variables

    I have about 20 variables that I need to clear the values of, and I need to do this multiple times. Is there a way to do this with a foreach loop?

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Foreach will only work if the item is in an array (and GLOBALS won't count :P) or is a traversable type of object.
    You can unset as many variables as you want though. That's a simple matter of calling unset with multiple arguments:
    PHP Code:
    unset($var1$var2$var3$etc); 
    The only alternative I can think of is to provide an array of keys to lookfor, and traverse the $GLOBALS and unset. I do not suggest that route though.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    cgdtalent (05-22-2013)

  • #3
    New Coder
    Join Date
    Jan 2013
    Posts
    74
    Thanks
    19
    Thanked 1 Time in 1 Post
    Quote Originally Posted by Fou-Lu View Post
    Foreach will only work if the item is in an array (and GLOBALS won't count :P) or is a traversable type of object.
    You can unset as many variables as you want though. That's a simple matter of calling unset with multiple arguments:
    PHP Code:
    unset($var1$var2$var3$etc); 
    The only alternative I can think of is to provide an array of keys to lookfor, and traverse the $GLOBALS and unset. I do not suggest that route though.
    Ok, unset will work better than doing $var1 = "", $var2 = "", etc...

    Thanks!

  • #4
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,073
    Thanks
    11
    Thanked 98 Times in 96 Posts
    ... or keep the variables in an array $ar['var1'] $ar['var2'] etc, then you just have to unset($ar);
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •