Re: [PHP] dynamically naming PHP vars on the fly?

2009-08-05 Thread Martin Scotta
You can use variable variables

?php

$nombre = 'Martin';
$name = 'nombre';

echo $$name; # === Martin

You can make more complicated statements with this technique.

$var1 = 'apple';
${ 'Fruit_' . $var1 } = 'organic';
echo $Fruit_apple; // here you are

When your statements are complex use the ${ statement } syntax.
I often use this for hidden global variables.

${ 'try to use this variable directly' } = 'something';

print_r( get_defined_vars() ); # [try to use this variable directly]
= something


On Wed, Aug 5, 2009 at 7:17 PM, Govindagovinda.webdnat...@gmail.com wrote:
 HI all

 One thing I have been working around but now would love to just do it
 finally (and save workaround/longer code hassle) is when:

 I need to be able to create a variable (name it, and assign it a value)
 whose name is built up from a fixed string concatenated with another string
 which comes from the  value of another (already set) variable.

 Ie:

 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?

 
 John Butler (Govinda)
 govinda.webdnat...@gmail.com




 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php





-- 
Martin Scotta

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] dynamically naming PHP vars on the fly?

2009-08-05 Thread Jerry Wilborn
http://us2.php.net/manual/en/language.variables.variable.php
Jerry Wilborn
jerrywilb...@gmail.com


On Wed, Aug 5, 2009 at 5:17 PM, Govinda govinda.webdnat...@gmail.comwrote:

 HI all

 One thing I have been working around but now would love to just do it
 finally (and save workaround/longer code hassle) is when:

 I need to be able to create a variable (name it, and assign it a value)
 whose name is built up from a fixed string concatenated with another string
 which comes from the  value of another (already set) variable.

 Ie:

 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?

 
 John Butler (Govinda)
 govinda.webdnat...@gmail.com




 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] dynamically naming PHP vars on the fly?

2009-08-05 Thread Govinda

You can use variable variables
...
You can make more complicated statements with this technique.

$var1 = 'apple';
${ 'Fruit_' . $var1 } = 'organic';
echo $Fruit_apple; // here you are


thank you all 3, for your help!
That was just what I wanted!

-Govinda

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php