SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: $$var ??

  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    $$var ??

    Can someone explain what that is and how it works? I have seen some code that has:

    PHP Code:
    $name='blah';
    $
    $name='blah2'
    Thanks..

  2. #2
    hi galen's Avatar
    Join Date
    Jan 2006
    Location
    New Haven, CT
    Posts
    1,228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it's a variable variable

    PHP Code:
    $name='blah';
    $
    $name='blah2'
    echo 
    $blah
    this will echo blah2

    the $name in $$name is assigned blah and you are left with $blah - setting $blah to 'blah2'

    more info

  3. #3
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,290
    Mentioned
    198 Post(s)
    Tagged
    3 Thread(s)

    variable variable

    It's a "variable variable". The PHP docs explain it better than I can, but it's something like
    $name='blah';/* $name var = 'blah' */
    $$name='blah2';/* same as $blah = 'blah2' */
    I've seen them used for dynamically created variable names, but I've never used them myself.

  4. #4
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I find them useful in classes where I frequenty have to iterate over a group of object members, without having to access them each individually

    e.g
    PHP Code:
    class something
    {
      public 
    $name;
      public 
    $email;
      public 
    $phone;
      public 
    $occupation;
      private 
    $members = array('name''email''phone''occupation');

      private function 
    trim_strings() {
         foreach(
    $this->members as $v) {
            
    $this->$v trim($this->$v);
        }
    }




Bookmarks

Posting Permissions

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