Building Variables In PHP

A question I often get is this: how can I make multiple variables into one? The problem is, you put them together, you go to call the variable, but instead of acting like a variable it acts like text. Here is one of the examples sent to me.

 $test = "success";
 $open = "$";
 $close = ";";
 $primary = "test";
 $ID = "$open$primary$close";
 echo $ID;

What the user wants is for the echo of $ID to be "success", but instead it is echoing "$test". What you need to do to make this work, is really very simple. Just put it in between {} symbols. For example:

 $test = "success";
 $primary = "test";
 $ID = ${$primary}; 
 echo $ID; 

Now when we execute the code, it is setting $ID equal to $test and therefor the output will be "success".

Coding Used

 

http://php.about.com/od/advancedphp/qt/build_variables.htm

Leave a comment