CSS-Tricks*

A Web Design Community curated by Chris Coyier

A little dab'll do ya

Code Snippets

Code Snippets > PHP > Variable Variables Submit one!

Variable Variables

<?php

  $var1 = 'nameOfVariable';

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

  echo $$var1; 

?>

Reference URL

Subscribe to The Thread

  1. Alexander Rath says:

    Mind you can also do that with more $s:

    And you can use curly brackets:

  2. Alexander Rath says:

    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 //

    ?>

It's Your Turn

At this moment, you have an awesome opportunity* to be the person your mother always wanted you to be: kind, helpful, and smart. Do that, and we'll give you a big ol' gold star for the day (literally).

Posting tips:
  • You can use basic HTML
  • When posting code, please turn all
    < characters into &lt;
  • If the code is multi-line, use
    <pre><code></code></pre>
Thank you,
--- The Management ---