Re: Question on variable variables
| From: | Christopher William Wesley | Date: | Sat, 17 Nov 2001 03:38:04 +0000 |
| Subject: | Re: Question on variable variables | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-74960@lists.php.net to get a copy of this message | ||
On Fri, 16 Nov 2001, Jeff Lewis wrote:
> What I need to do, however, is append a variable portion to a constant prefix. So I have a set
> of variables that are named $MYdog, $MYcat etc. and I need to do
>
> $a = "dog"
> ${"MY$a"} being the same as $MYdog
>
> Can this be done, and if so - how? I can't get it to work.
So what you want is a base name with a vaiably-changing portion
concatenated? Gotcha!
// you variable base name
$myVarBase = "MY";
$a = cat;
$something1 = "This Var Likes Cats";
$b = dog;
$something2 = "This Var Likes Dogs";
$myVarName1 = $myVarBase . $a;
$myVarName2 = $myVarBase . $b;
// The same as $Mycat = "This Var Likes Cats";
${$myVarName1} = $something1;
// The same as $Mydog = "This Var Likes Dogs";
${$myVarName2} = $something2;
g.luck
~Chris /"\
\ / September 11, 2001
X We Are All New Yorkers
/ \ rm -rf /bin/laden