SitePoint Sponsor |
|
User Tag List
Results 1 to 4 of 4
Thread: $$var ??
-
Dec 16, 2007, 20:58 #1
- 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';
-
Dec 16, 2007, 21:17 #2
it's a variable variable
PHP Code:$name='blah';
$$name='blah2';
echo $blah;
the $name in $$name is assigned blah and you are left with $blah - setting $blah to 'blah2'
more info
-
Dec 16, 2007, 21:17 #3
- 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.Big Change Coming Soon - if you want your PMs save them now!
What you need to do to prepare for our migration to Discourse
A New SitePoint Forum Experience: Our Move to Discourse
-
Dec 16, 2007, 23:50 #4
- 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