variable variable names
Posted: Fri Nov 25, 2005 9:45 am
Hi is it possible to name a variable with another vairable?
e.g
e.g
Code: Select all
$name = "john";
$($john) = "hello";A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
Code: Select all
$name = "john";
$($john) = "hello";Code: Select all
$foo = "hello world";
$bar = "foo";
$car = "bar";
$dar = "car";
echo $$$$dar;I find that completely useless, since you can just as well put the stuff from Step 5 into an array with the name of the input field as the key. It's a lot more elegant and easier to understand.onion2k wrote: I wrote a short article about them .. http://www.ooer.com/index.php?section=php&id=5
Code: Select all
for($i=1;$i<5;$i++)
{
${"var$i"} = "this is my variable";
}Hence I followed step 5 with "This is a rather trivial use of references". The point I was demonstrating was that you can use references to programmatically create variable names. Without going into quite long and detailed code it's tricky to come up with a useful example .. so I left it at something basic.foobar wrote:I find that completely useless, since you can just as well put the stuff from Step 5 into an array with the name of the input field as the key. It's a lot more elegant and easier to understand.onion2k wrote: I wrote a short article about them .. http://www.ooer.com/index.php?section=php&id=5
Good point, but I still stand firm in my belief that variable variables are the devil incarnateonion2k wrote:Without going into quite long and detailed code it's tricky to come up with a useful example .. so I left it at something basic.foobar wrote:I find that completely useless, since you can just as well put the stuff from Step 5 into an array with the name of the input field as the key. It's a lot more elegant and easier to understand.onion2k wrote: I wrote a short article about them .. http://www.ooer.com/index.php?section=php&id=5
I agree with this completely. Variable variables I consider the devil aswell, as the programmer may become unsure as the where the variable came from. From what I can tell variable variables generally eliminate the need to manually declare the variables, much like extract().. but in those instances I find it much more clear to either have an array created of the related variables if it is already not an array you are working with.. or leave it as an array in the first place.foobar wrote:Good point, but I still stand firm in my belief that variable variables are the devil incarnateonion2k wrote:Without going into quite long and detailed code it's tricky to come up with a useful example .. so I left it at something basic.foobar wrote: I find that completely useless, since you can just as well put the stuff from Step 5 into an array with the name of the input field as the key. It's a lot more elegant and easier to understand.. I'll just leave it at that, there's no point in going into another "OOP vs. Procedural" type of flamewar here.
Code: Select all
$sql = "select * from config_values";
$res = mysql_query($sql);
while($data = mysql_fetch_array($res))
{
$$data['name'] = $data['value'];
}Code: Select all
define($name, $value);It depends on your definition of a config value.jshpro2 wrote:Now they are avail on a true global level, and cannot be changed (perfect for a config value)
Interesting line of thought. To be honest, I never thought of it that way. Hmm.jshpro2 wrote:On a single page request a "config" variable does not need to be changed, by my definition, the only time a changing variable would be considered a config var to me is if the application was something like a shell script that runs 24/7. Think about it, in a traditional web application the only way a config variable would be changed, is the user changing the config variable, wether it be done in a seperate page request or through AJAX, one process changes the config variable in the database, the next process reads that config value out.
Code: Select all
$CONFIGURE['mysql_password'] = 'foo';Code: Select all
global $CONFIGURE