Page 1 of 1

referer to a variable from a string [SOLVED]

Posted: Fri Jan 06, 2006 9:03 am
by kendall
Hi,

I would like to know if there is a way to referer to an already instantiated variable by using a string..

for example

there is a variable named $brokerName = 'The Name';
and i have a variable names $name = 'broker';
can i get the value of $brokerName by saying echo eval("$".$name.'Name');
?

Kendall

Posted: Fri Jan 06, 2006 9:06 am
by Jenk

Code: Select all

${$broker . 'Name'};
Lookup variable variables on php.net for more examples :)

referer to a variable from a string[SOLVED]

Posted: Fri Jan 06, 2006 9:14 am
by kendall
Jenk wrote:

Code: Select all

${$broker . 'Name'};
Lookup variable variables on php.net for more examples :)
so would it be safe to say that

Code: Select all

${$name.'Name'};
would give me the value of $brokerName
The Name
?

i tried your solution but i dont think its working

I should mention that $name is being taken out of an array thus the value changes but referes to a already instantiated variable when combines with the string Name

Kendall

sorry....i got it to worked

Posted: Fri Jan 06, 2006 9:35 am
by Jenk

Code: Select all

<?php

$HelloWorld = "Hiya Earth!";

$one = "Hello";

$two = "World";

echo ${$one . $two}; //outputs "Hiya Earth!";

?>