PHP Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 3.00

Variable Variable Name - Name Variables with Expressions

This section describes what is a variable variable name, storing the variable name in another variable, naming variables with expressions, using any expressions as variable names.

What is a Variable Variable Name: A variable variable name is a variable name that is stored inside another variable. Normally, a variable name is a static string. But sometimes, you want to store a variable name inside another variable to be able to participate in operations. PHP supports variable variable names with following basic rules:

1. If the name of the original variable is stored in a different variable, you can use the $$var notation to identify the original variable in your PHP source code. For example, $variableName = "title"; $$variableName represents the variable $title.

2. If the name of the original variable can be calculated by an expression, you can use the ${exp} notation to identify the original variable in your PHP source code. For example, $variableName = "title"; ${$variableName."_1"} represents the variable $title_1.

3. Variables with variable names can be assigned with new data like normal variables. For example, $variableName = "title"; $$variableName = "Herong's PHP Book"; assigns "Herong's PHP Book" to $title.

4. Variables with variable names can be used in any operations like normal variables. $variableName = "title"; $$variableName = "Herong's PHP Book"; print($$variableName." is free"); prints "Herong's PHP Book is free".

5. If you use an expression as the variable name, the result of the expression will be automatically converted to a string, which will be used as the variable name. With this automatic conversion, anything can be used as a variable name. Even an empty string can be used as a variable name, ${""} is a perfect variable expression.

Now let's look at my tutorial example below to confirm some rules mentioned above:

<?php # VariableVariableName.php
# Copyright (c) 2005 by Dr. Herong Yang. http://www.herongyang.com/
#
   $variableName = "title"; 
   $$variableName = "Herong's PHP Book";
   print "\n \$\$variableName is assigned to a message:\n";
   print "    \$variableName: "; var_dump($variableName);
   print "    \$\$variableName: "; var_dump($$variableName);
   print "    \$title: "; var_dump($title);

   $variableName = ""; 
   $$variableName = "Herong's Programming Book";
   print "\n \$variableName contains an empty string:\n";
   print "    \$variableName: "; var_dump($variableName);
   print "    \$\$variableName: "; var_dump($$variableName);
   print "    \${''}: "; var_dump(${''});

   $variableName = NULL; 
   $$variableName = "Herong's Tutorial Book";
   print "\n \$variableName contains the NULL:\n";
   print "    \$variableName: "; var_dump($variableName);
   print "    \$\$variableName: "; var_dump($$variableName);
   print "    \${NULL}: "; var_dump(${NULL});

   $variableName = 1.0/3.3; 
   $$variableName = "Herong's Free Book";
   print "\n \$variableName contains a float number:\n";
   print "    \$variableName: "; var_dump($variableName);
   print "    \$\$variableName: "; var_dump($$variableName);
   print "    \${1.0/3.3}: "; var_dump(${1.0/3.3});
?>

If you run this sample script, you should get:

 $$variableName is assigned to a message:
    $variableName: string(5) "title"
    $$variableName: string(17) "Herong's PHP Book"
    $title: string(17) "Herong's PHP Book"

 $variableName contains an empty string:
    $variableName: string(0) ""
    $$variableName: string(25) "Herong's Programming Book"
    ${''}: string(25) "Herong's Programming Book"

 $variableName contains the NULL:
    $variableName: NULL
    $$variableName: string(22) "Herong's Tutorial Book"
    ${NULL}: string(22) "Herong's Tutorial Book"

 $variableName contains a float number:
    $variableName: float(0.30303030303)
    $$variableName: string(18) "Herong's Free Book"
    ${1.0/3.3}: string(18) "Herong's Free Book"

Last update: 2005.

Sections in This Chapter

Variables and Assignment Operations

References and Variables

Variable Variable Name - Name Variables with Expressions

Constant and define() Function

Dr. Herong Yang, updated in 2009
Variable Variable Name - Name Variables with Expressions