Re: dynamically naming PHP vars on the fly?
| From: | Nisse Engström | Date: | Thu, 06 Aug 2009 07:20:54 +0000 |
| Subject: | Re: dynamically naming PHP vars on the fly? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-296371@lists.php.net to get a copy of this message | ||
On Wed, 5 Aug 2009 16:17:26 -0600, Govinda wrote:
> I want to do this:
> (I am just assuming it won't work; I haven't even tried it yet)
>
> $var1='apple';
> $Fruit_$var1="organic";
> echo "$Fruit_apple"; // I want this to return "organic"
>
> Or how are you guys dynamically naming PHP vars on the fly?
Others have mentioned variable variables. While I have
used those, I tend to prefer arrays:
$var1 = 'apple';
$fruits[$var1] = 'organic';
echo $fruits[$var1];
/Nisse