Skip to content

Instantly share code, notes, and snippets.

@hellofromtonya
Created April 27, 2015 16:57
Show Gist options
  • Save hellofromtonya/d2b02f338f21e3a3ee44 to your computer and use it in GitHub Desktop.
Save hellofromtonya/d2b02f338f21e3a3ee44 to your computer and use it in GitHub Desktop.
Variable Variables Tutorial in WordPress
<?php
$variable_name = 'post_id';
$$variable_name = 10;
//* Or use curly braces to specify exactly which variable is being used
//* as the variable variable, i.e. variable name
${$variable_name} = 10;
/**
* Think of it like this:
*
* 1. $variable_name holds the variable name that you will use "later"
* to access or create a variable.
* 2. In ${$variable_name}, replace what is in the curly braces with
* the "assignment" in the variable $variable_name, which here is 'post_id'.
*/
//* Now let's see what assignments were made
echo '$$variable_name = ';
var_dump( $$variable_name );
//* Notice that I did not declare the variable $post_id;
//* however, PHP did create it...dynamically.
echo '$post_id = ';
var_dump( $post_id );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment