Variable Variables

<?php

  $var1 = 'nameOfVariable';

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

  echo $$var1; 

?>

Comments

  1. User Avatar
    Alexander Rath
    Permalink to comment#

    Mind you can also do that with more $s:

    And you can use curly brackets:

  2. User Avatar
    Alexander Rath
    Permalink to comment#

    Mind you can also do that with more $s:

    <?php
    
      $var1 = 'var2';
      $var2 = 'nameOfVariable';
    
      $nameOfVariable = 'The value I want!';
    
      echo $$$var1;
    
    ?>

    And you can use curly brackets:

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

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

  5. User Avatar
    Attila Hajzer

    how? ^

  6. User Avatar
    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.

    • User Avatar
      Nerve

      $var1=$_POST’username’];
      echo “Hello [” . $var1 . “]”;

  7. User Avatar
    sreevisakh

    Try like this

    $var=$_POST[‘username’];
    echo ‘Hello[‘.$var.’]’;

  8. User Avatar
    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' => 'baylor@example.com'));
    echo $name; // Baylor
    echo $email; // baylor@example.com
    
    // example: http://baylorrae.codepad.org/VdIA118O
    

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

    <?php
    
    list($name, $email) = array('Baylor', 'baylorrae@gmail.com');
    
    echo $name;
    echo $email;
    
    // example: http://baylorrae.codepad.org/WejSCaCB
    
  9. User Avatar
    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. User Avatar
    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. User Avatar
    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.

Submit a Comment

Posting Code

You may write comments in Markdown. This makes code easy to post, as you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences (```) with double new lines before and after.

Code of Conduct

Absolutely anyone is welcome to submit a comment here. But not all comments will be posted. Think of it like writing a letter to the editor. All submitted comments will be read, but not all published. Published comments will be on-topic, helpful, and further the discussion or debate.

Want to tell us something privately?

Feel free to use our contact form. That's a great place to let us know about typos or anything off-topic.

icon-closeicon-emailicon-linkicon-logo-staricon-menuicon-searchicon-staricon-tag