The Lodge is members-only design/dev videos and Office Hours.

Next Office Hours Session: "Programming Fundamentals" Dec 02 - 2:00 PM Eastern

Variable Variables

Last updated on:

  $var1 = 'nameOfVariable';

  $nameOfVariable = 'This is the value I want!';

  echo $$var1; 


Reference URL


  1. Alexander Rath
    Permalink to comment#

    Mind you can also do that with more $s:

    And you can use curly brackets:

  2. Alexander Rath
    Permalink to comment#

    Mind you can also do that with more $s:

      $var1 = 'var2';
      $var2 = 'nameOfVariable';
      $nameOfVariable = 'The value I want!';
      echo $$$var1;

    And you can use curly brackets:

      $var1 = 'nameOf';
      $var2 = 'TheVariable';
      $nameOfTheVariable = 'Another value I want!';
      echo ${$var1 . $var2}; // Use concatenation to generate the variable name and then access it //
  3. sas
    Permalink to comment#
  4. John Shammas

    Wow! I never even knew about this. Might be a pretty great asset in the future.

  5. Attila Hajzer

    how? ^

  6. Mahesh

    I would like to ask a question, How to show variable in between the text.
    E.g., I have
    “var1 = $_POST[‘username’];”

    I want to show it something like this,
    “Hello [username]”.

    How to do it?

    I tried this but it didn’t work,
    “echo Hello $var1;”

    Please help me regarding this.

    • Nerve

      echo “Hello [” . $var1 . “]”;

  7. sreevisakh

    Try like this

    echo ‘Hello[‘.$var.’]’;

  8. Baylor Rae'
    Permalink to comment#

    If you need to create variables from an array there are two options.

    The first uses php extract()

     'Baylor', 'email' => ''));
    echo $name; // Baylor
    echo $email; //
    // example:

    The second options which will give you more control is php list()

    list($name, $email) = array('Baylor', '');
    echo $name;
    echo $email;
    // example:
  9. perpleXa
    Permalink to comment#

    this is really bad practice and with proper code you should not need that mutation of code.

    also, don’t directly output $_POST/$_GET or $_REQUEST content without using htmlentities() around it, as your site might be vulnerable to cross-site-scripting and other injections.

  10. Chris
    Permalink to comment#

    Hmmm, I’m probably just a little dense, but could you give an example of when this technique would be useful?

  11. NogZoiets
    Permalink to comment#

    I don’t believe this piece of code would be useful in any way. When coding in PhP or any other language, one of the things you should keep in mind is that your code should be readable by others.
    Of course the content of your code, the values of the var’s, shouldn’t be.

    But when using a snippet like this, you take a away the readability. So i have to agree with perpleXa that this is a bad practice. Srry.

Leave a Comment

Posting Code

We highly encourage you to post problematic HTML/CSS/JavaScript over on CodePen and include the link in your post. It's much easier to see, understand, and help with when you do that.

Markdown is supported, so you can write inline code like `<div>this</div>` or multiline blocks of code in in triple backtick fences like this:

  function example() {
    element.innerHTML = "<div>code</div>";

There's a whole bunch of content on CSS-Tricks.

Search for Stuff   •   Browse the Archives

Get the Newsletter ... or get the RSS feed