Page 1 of 2

variable variable names

Posted: Fri Nov 25, 2005 9:45 am
by php3ch0
Hi is it possible to name a variable with another vairable?

e.g

Code: Select all

$name = "john";

$($john) = "hello";

Posted: Fri Nov 25, 2005 9:52 am
by foobar
Yes!
PHP Manual wrote:
<?php
$a = "Hello";
?>

...

<?php
$$a = "World";
?>

Posted: Fri Nov 25, 2005 11:18 am
by Grim...
To clarify: In foobars example, $Hello would equal "World".

Posted: Fri Nov 25, 2005 2:59 pm
by onion2k
You can reference variables to more than one level too..

Code: Select all

$foo = "hello world";
$bar = "foo";
$car = "bar";
$dar = "car";
echo $$$$dar;
I wrote a short article about them .. http://www.ooer.com/index.php?section=php&id=5

Posted: Fri Nov 25, 2005 3:19 pm
by foobar
onion2k wrote: I wrote a short article about them .. http://www.ooer.com/index.php?section=php&id=5
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.

Posted: Fri Nov 25, 2005 3:30 pm
by Burrito
try something like this:

Code: Select all

for($i=1;$i<5;$i++)
{
  ${"var$i"} = "this is my variable";
}

Posted: Fri Nov 25, 2005 4:06 pm
by onion2k
foobar wrote:
onion2k wrote: I wrote a short article about them .. http://www.ooer.com/index.php?section=php&id=5
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.
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.

Posted: Fri Nov 25, 2005 4:15 pm
by foobar
onion2k wrote:
foobar wrote:
onion2k wrote: I wrote a short article about them .. http://www.ooer.com/index.php?section=php&id=5
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.
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.
Good point, but I still stand firm in my belief that variable variables are the devil incarnate :P . I'll just leave it at that, there's no point in going into another "OOP vs. Procedural" type of flamewar here. :wink:

Posted: Fri Nov 25, 2005 4:23 pm
by John Cartwright
foobar wrote:
onion2k wrote:
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.
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.
Good point, but I still stand firm in my belief that variable variables are the devil incarnate :P . I'll just leave it at that, there's no point in going into another "OOP vs. Procedural" type of flamewar here. :wink:
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.

Posted: Fri Nov 25, 2005 6:33 pm
by trukfixer
one place I find it useful is if you store configuration data in a database.. stuff like webmaster email, site name, etc whatever , in a database structure like index_id,name,value,description
then you can load it by a simple query in config like

Code: Select all

$sql = "select * from config_values";
$res = mysql_query($sql);
while($data = mysql_fetch_array($res))
{
     $$data['name'] = $data['value'];
}
and suddenly you have a configuration table where you can change settings on the fly via an admin panel, (a la phpBB) instead of having to manually edit and upload a configuration file (and the chances of a typo or extra whitespace causing your beautiful new website to blow up with ugly error messages all over the place just when you are getting the most traffic of your life.. how embarrassing..)

Posted: Fri Nov 25, 2005 7:56 pm
by josh
trukfixer, what advantages does this have over just setting your config valuues like this?

Code: Select all

define($name, $value);
Now they are avail on a true global level, and cannot be changed (perfect for a config value)

Posted: Fri Nov 25, 2005 8:53 pm
by Roja
jshpro2 wrote:Now they are avail on a true global level, and cannot be changed (perfect for a config value)
It depends on your definition of a config value.

In BNT, we want *all* config variables (except the db setting) to be able to be changed realtime, so define is horrible. :)

Posted: Fri Nov 25, 2005 8:58 pm
by josh
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.

Posted: Fri Nov 25, 2005 9:24 pm
by Roja
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.
Interesting line of thought. To be honest, I never thought of it that way. Hmm.

Posted: Fri Nov 25, 2005 9:57 pm
by josh
If you really needed your 'config' vars to be editable at runtime, it's a more solid solution to use a master array for configs, as opposed to variable variables (which can lead to issues)

Code: Select all

$CONFIGURE['mysql_password'] = 'foo';
Now to access config vars within any scope

Code: Select all

global $CONFIGURE
However define is probably the more practical solution since it's truly global!


Afterthought:

debugging = print_r($CONFIGURE); :D

Each approach has it's benefits, for instance a heirarchial config tree using the $CONFIGURE array multidemensionally, (ex. keep all mysql related directives in their own sub array)