PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
bokehman
Forum Regular
Posts: 509 Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)
Post
by bokehman » Tue Jun 14, 2005 5:13 am
I have a variable named $string. It contains something like abc (letters only). I want a new variable named after its content. eg:
Code: Select all
$string = abc;
//I want a new variable that looks like this:
$abc
shiznatix
DevNet Master
Posts: 2745 Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:
Post
by shiznatix » Tue Jun 14, 2005 5:34 am
use arrays
Code: Select all
$arr = array();
$arr['string'] = 'abc';
$arr[$arr['string']] = 'what you want the $arr[abc] to equal';
onion2k
Jedi Mod
Posts: 5263 Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com
Post
by onion2k » Tue Jun 14, 2005 5:49 am
Err.. use references.
Code: Select all
$string = "abc";
$abc = "hello";
echo $$string; // Note the 2 $ signs.
shiznatix
DevNet Master
Posts: 2745 Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:
Post
by shiznatix » Tue Jun 14, 2005 6:33 am
o wow i never knew you could do that. learn somtin new every day!
timvw
DevNet Master
Posts: 4897 Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium
Post
by timvw » Tue Jun 14, 2005 6:34 am
It's a matter of reading the manual
Chapter on variables... Section variable variables
Code: Select all
$name = "tim";
${$name} = "vw";
echo $tim;
Syranide
Forum Contributor
Posts: 281 Joined: Fri May 20, 2005 3:16 pm
Location: Sweden
Post
by Syranide » Tue Jun 14, 2005 7:04 am
be careful what you do though, doing such thing on a $_GET-variable could seriously compromise passwords and so on.