RE: [PHP] $ vs $$
| From: | MR | Date: | Sun, 19 Nov 2000 14:18:03 +0000 |
| Subject: | RE: [PHP] $ vs $$ | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-26147@lists.php.net to get a copy of this message | ||
> I have seen places where $$ is used. When should I use $$ and what does it do.
>
> Thank you.
> Dallas K.
Well, you may know the simple way that PHP uses to go through arrays, specially through the
HTTP-posted variables:
while ( list ($name,$value) = each ($HTTP_POST_VARS) )
{
(code)
}
You may know, every http-posted var has a name and a value. Well. That simple (but very often used)
line of code goes throughall the
posted variables; it assigns the name of the posted var to $name, and the value of the posted var to
$value.
Keep in mind that $name and $value are just temporary, and in the next loop they'll change.
Well, if you use $value for calculations, and modify it, when the program finishes the *while*
sentence, your modified values won't
be seen *anywhere* as the $value variable wasn't linked with the "real" posted
variables.
But if you use $$name instead...
You are saying: "the value of the variable named $name". If you modify that ($$name)
instead of $value, the variable named $name
will have its contents changed out of the *while* sentence....
I've been using that often with that damned http-posted variables. For example, for search and
replace ( str_replace() ) the
contents of every http-posted variables.