1. Home
  2. Computing & Technology
  3. PHP / MySQL

Building Variables In PHP
How can I build a variable from other variables?

By Angela Bradley, About.com

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".
More PHP / MySQL Quick Tips
Explore PHP / MySQL
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. PHP / MySQL
  4. Advanced PHP
  5. Build Variables - Dynamic Variables - Variables on the Fly

©2009 About.com, a part of The New York Times Company.

All rights reserved.