Page 1 of 1

Name a variable after the content of another variable

Posted: Tue Jun 14, 2005 5:13 am
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

Posted: Tue Jun 14, 2005 5:34 am
by shiznatix
use arrays

Code: Select all

$arr = array();

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

$arr[$arr['string']] = 'what you want the $arr[abc] to equal';

Posted: Tue Jun 14, 2005 5:49 am
by onion2k
Err.. use references.

Code: Select all

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

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

Code: Select all

Hello

Posted: Tue Jun 14, 2005 6:33 am
by shiznatix
o wow i never knew you could do that. learn somtin new every day!

Posted: Tue Jun 14, 2005 6:34 am
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;

Posted: Tue Jun 14, 2005 7:04 am
by Syranide
be careful what you do though, doing such thing on a $_GET-variable could seriously compromise passwords and so on.