p2p.wrox.com Forums

Need to download code?

View our list of code downloads.

  Return to Index  

beginning_php thread: $$

Message #1 by "John Arbon" <subscriptions@c...> on Tue, 9 Apr 2002 05:38:04
Alright!  I encounter an oddity (to me).

What does a $$ do in PHP?

ie: $$key = $val;

Message #2 by "Nikolai Devereaux" <yomama@u...> on Mon, 8 Apr 2002 23:43:36 -0700
It's called a "variable variable."  Let me show you how it works:

1  $yourName  = "John";
2  $$yourName = "Some guy.";
4  echo $John;  // prints "Some guy."

See, what happens on line 2 is that the $yourName gets evaluated to "John",
but you still have another dollar sign there!  Hence, $John is created and
set to "Some guy."

It's a really cool feature that can be extremely useful if used in the right
places, but I've seen a lot of code that uses this feature poorly.

I've mentioned it before, so I might as well do it again.  Don't be tempted
to use a cool trick as part of the solution to a problem unless it really
does make sense.  In other words, don't come up with the solutions to your
problems with specific functions or tricks in mind; do it the other way
around.  Tell yourself how you plan on solving the problem, and the
functions you need will become obvious.

Take care,


Message #3 by "Cirstoiu Aurel Sorin" <csorin@i...> on Tue, 9 Apr 2002 10:15:33 +0300
Here is a useful link:

Cirstoiu Aurel Sorin
Interakt Online Support

----- Original Message -----
From: "John Arbon" <subscriptions@c...>
To: "beginning php" <beginning_php@p...>
Sent: Tuesday, April 09, 2002 5:38 AM
Subject: [beginning_php] $$

> Alright!  I encounter an oddity (to me).
> What does a $$ do in PHP?
> ie: $$key = $val;
> John

Message #4 by "John Arbon" <subscriptions@c...> on Tue, 9 Apr 2002 21:01:08

So, I could take an array of words and create variables out of them by 
using this particular feature in PHP?  Have I got that right?


BTW, I'm attemping to understand some BBS software in order to re-write 
parts of it to fit into my site more seamlessly...
Message #5 by "Nikolai Devereaux" <yomama@u...> on Tue, 9 Apr 2002 13:04:04 -0700
Yes, that's right.

from your brief original post, I'm assuming the code looked something like

while(list($key, $val) = each($arr))
    $$key = $val;

This means that if you had this:

$people = array('nik'  => 'nikolai@b...',
                'john' => 'subscriptions@c...',
                'jake' => 'cohen@r...',
                'dan'  => 'dan@t...',
                'pete' => 'peter@p...');

and ran it through that while loop (substituting $arr for $people)

you'd have these variables defined:

$nik  = 'nikolai@b...';
$john = 'subscriptions@c...';
$jake = 'cohen@r...';
$dan  = 'dan@t...';
$pete = 'peter@p...';


  Return to Index