referer to a variable from a string [SOLVED]

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
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

referer to a variable from a string [SOLVED]

Post 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
Last edited by kendall on Fri Jan 06, 2006 10:07 am, edited 1 time in total.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Code: Select all

${$broker . 'Name'};
Lookup variable variables on php.net for more examples :)
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

referer to a variable from a string[SOLVED]

Post 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
Last edited by kendall on Fri Jan 06, 2006 9:49 am, edited 1 time in total.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Code: Select all

<?php

$HelloWorld = "Hiya Earth!";

$one = "Hello";

$two = "World";

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

?>
Post Reply