Connecting Tech Pros Worldwide Forums | Help | Site Map

Dynamic Variable Name $$var

Angelos
Guest
 
Posts: n/a
#1: Mar 14 '06
Hello,

I need to dynamically specify the name of a variable. I just read that
$varA = "Cat";
echo $$varA;

OUTPUT: Cat

What I try to establish is somehow add a bit of text on my dynamic
variable name.
so:

$varA = "Cat";
$$varA.'Food' <------ I am not sure what should be the syntax in order
the variable name to be $CatFood

The reason I want to do that is because I take the first part of the
variable name from a Database.

Thanks.

Angelos
Guest
 
Posts: n/a
#2: Mar 14 '06

re: Dynamic Variable Name $$var


Angelos wrote:[color=blue]
> Hello,
>
> I need to dynamically specify the name of a variable. I just read that
> $varA = "Cat";
> echo $$varA;
>
> OUTPUT: Cat[/color]
SOrry That was wrong example;
THats a correct one;

$varA = "dog";
$$varA = "jack";
echo "Dog name is: ".$dog;
OUTPUT: Dog name is: jack[color=blue]
>
> What I try to establish is somehow add a bit of text on my dynamic
> variable name.
> so:
>
> $varA = "Cat";
> $$varA.'Food' <------ I am not sure what should be the syntax in order
> the variable name to be $CatFood
>
> The reason I want to do that is because I take the first part of the
> variable name from a Database.
>
> Thanks.[/color]
Angelos
Guest
 
Posts: n/a
#3: Mar 14 '06

re: Dynamic Variable Name $$var


I worked it out I think:

$a = "a";
$b = "b";
$varA = $a.$b;

$$varA = "jack";
echo "Dog name is: ".$ab;
Kimmo Laine
Guest
 
Posts: n/a
#4: Mar 14 '06

re: Dynamic Variable Name $$var


"Angelos" <angelos@redcatmedia.net> wrote in message
news:dv68u4$ba4$1@nwrdmz01.dmz.ncs.ea.ibs-infra.bt.com...[color=blue]
>I worked it out I think:
>
> $a = "a";
> $b = "b";
> $varA = $a.$b;
>
> $$varA = "jack";
> echo "Dog name is: ".$ab;[/color]

You can also use the curly braces so you don't need to always assign a new
variable for that. Example:

$alpha = 'a';
$beta = 'b';
${$alpha.$beta} = 'all your phps are belong to us';
echo $ab;

--
"En ole paha ihminen, mutta omenat ovat elinkeinoni." -Perttu Sirviö
spam@outolempi.net | Gedoon-S @ IRCnet | rot13(xvzzb@bhgbyrzcv.arg)


Ridge Burner
Guest
 
Posts: n/a
#5: Mar 14 '06

re: Dynamic Variable Name $$var


> ${$alpha.$beta} = 'all your phps are belong to us';

Now THAT is funny!


Angelos
Guest
 
Posts: n/a
#6: Mar 14 '06

re: Dynamic Variable Name $$var


Kimmo Laine wrote:[color=blue]
> "Angelos" <angelos@redcatmedia.net> wrote in message
> news:dv68u4$ba4$1@nwrdmz01.dmz.ncs.ea.ibs-infra.bt.com...
>[color=green]
>>I worked it out I think:
>>
>>$a = "a";
>>$b = "b";
>>$varA = $a.$b;
>>
>>$$varA = "jack";
>>echo "Dog name is: ".$ab;[/color]
>
>
> You can also use the curly braces so you don't need to always assign a new
> variable for that. Example:
>
> $alpha = 'a';
> $beta = 'b';
> ${$alpha.$beta} = 'all your phps are belong to us';
> echo $ab;
>[/color]
OHhh thats Great !!!
Thanks .... Much better way of doing the same thing. ;)
Closed Thread