Page 1 of 1
Simple Variable Question
Posted: Tue Aug 24, 2004 10:54 pm
by Pineriver
Hi,
Obviously this code will generate an error, but might anyone know how to acquire this result?
Code: Select all
$tempvar = 'secondhalf';
$foo.$tempvar = "This is the value that will be put in the var '$foosecondhalf'.";
print "$foosecondhalf"; //would print "This is the value that will be put in the var '$foosecondhalf'."
Thanks
Posted: Tue Aug 24, 2004 11:09 pm
by feyd
${'foo' . $tempvar} = "blahlbah";
for future generations a keyword: metaprogramming
Huh
Posted: Thu Aug 26, 2004 10:18 am
by neophyte
I don't get it? Why the braces?..... Can you explain Feyd
Posted: Thu Aug 26, 2004 10:36 am
by feyd
it has to do with how the variables are replaced and sorted out.. $foo.$tempvar concatenates the string results of 2 variables and returns a string, where mine, concatenates the two into a string
before the variable part is hooked.. so a string is passed to the first $ there thus creating the variable.
reading this may help:
http://www.php.net/manual/en/language.v ... riable.php
Yeah I think I get it...
Posted: Thu Aug 26, 2004 9:40 pm
by neophyte
I've been wondering if there was a way to do this... I found this posted on the variable variables link you put up Feyd. Good practical example of Variable Variables. This takes post variables and names the variable the key name (with a prefix) and assigns its value to it. Great way of going from $_POST['some_variable']="blah" to $some_variable = "blah"... etc....
(Did I over explain that?)
Code: Select all
foreach($_POST as $key => $value) {
$varname = "frm_".$key;
$$varname = $value;
}
Thanks Feyd

Posted: Thu Aug 26, 2004 10:43 pm
by feyd
that's basically, a manual [php_man]extract[/php_man] call.

Posted: Fri Aug 27, 2004 3:26 am
by Lord Sauron
By the way, why is this subject categorized under 'Theory and Design'?