1

Possible Duplicate:
Appending a value of a variable to a variable name?

I can't figure out the syntax at all and have searched far and wide.

I would like to do this:

$uni = "ntu";
$selectedntu = "something";

echo $selected$uni;
// output should be the same as
echo $selectedntu;

In other words I'd like to use the contents of the second variable $uni to join onto the first variable's name. $selectedntu has been set with a foreach loop, but I can't figure out how to reference the two variables together in php.

Community
  • 1
  • 1
patrickzdb
  • 928
  • 1
  • 10
  • 26
  • Nevermind I found the answer here: http://stackoverflow.com/questions/1774975/appending-a-value-of-a-variable-to-a-variable-name ${'selected'.$uni}; – patrickzdb Oct 09 '12 at 18:07
  • Have you also read the manual chapter about [arrays](http://php.net/array) yet? Because that's one of the common causes for newcomers to inquire about var varnames. – mario Oct 09 '12 at 18:08

1 Answers1

4

Construct the string and use a variable variable $$

$uni = "ntu";
$selected = "something";

$new_variable = $selected . $uni; 
echo $$new_variable;

// Or..per your googleing..purely for reference for others later

$uni = "ntu";
$selected = "something";

echo ${$selected . $uni};  
wesside
  • 5,622
  • 5
  • 30
  • 35