Name a variable after the content of another variable

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

Post Reply
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Name a variable after the content of another variable

Post by bokehman »

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
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

use arrays

Code: Select all

$arr = array();

$arr['string'] = 'abc';

$arr[$arr['string']] = 'what you want the $arr[abc] to equal';
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Err.. use references.

Code: Select all

$string = "abc";
$abc = "hello";

echo $$string; // Note the 2 $ signs.

Code: Select all

Hello
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

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 »

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 »

be careful what you do though, doing such thing on a $_GET-variable could seriously compromise passwords and so on.
Post Reply