-4

I have a variable and I want to create a variable with that. I get the variable from database and put it together with some text and then I want another variable.

For exampel

$a = $ . "txt" . $d;
Anders
  • 160
  • 1
  • 1
  • 9
  • 2
    That's nice. How about asking a question though? – James Coyle Mar 05 '13 at 18:07
  • 2
    (1) you should NEVER need this. It destroys all discoverability for other developers, (2) just use an array (3) `$a = ${'txt'.$d};` – Wrikken Mar 05 '13 at 18:07
  • PHP calls those "variable variables" [Here are the relevant docs](http://php.net/manual/en/language.variables.variable.php) – Michael Berkowski Mar 05 '13 at 18:08
  • to Wrikken. it is because i get the the stuff from the database and then i need to relate to another variable with that one. – Anders Mar 05 '13 at 18:12
  • @Anders Wrikken is right. The only reason you should use variable variables would be if the variable already had been defined e.g. `$txt_3 = "Hello World!"` and then you need to call the variable like so: `echo ${"txt_".$row['field']}` assuming that `$row['field'] == 3` – Corfitz. Mar 05 '13 at 18:20

1 Answers1

4

Try with this. It will create a variable from another one.

$a = ${'txt'.$d}

P.s. This is a question asked a couple of times. You might have found the answer simply by searching the issue on google.

Corfitz.
  • 1,864
  • 3
  • 31
  • 53