PHP Tutorials - Herong's Tutorial Examples - v5.17, by Herong Yang
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 2003 (c) HerongYang.com. All Rights Reserved. # $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:
herong> \php\php VariableVariableName.php $$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"
Table of Contents
Introduction and Installation of PHP
PHP Data Types and Data Literals
►Variables, References, and Constants
Variables and Assignment Operations
►Variable Variable Name - Name Variables with Expressions
Constant and define() Function
Expressions, Operations and Type Conversions
Conditional Statements - "if" and "switch"
Loop Statements - "while", "for", and "do ... while"
Function Declaration, Arguments, and Return Values
Interface with Operating System
Introduction of Class and Object
Integrating PHP with Apache Web Server
Retrieving Information from HTTP Requests
Creating and Managing Sessions in PHP Scripts
Sending and Receiving Cookies in PHP Scripts
Controlling HTTP Response Header Lines in PHP Scripts
MySQL Server Connection and Access Functions
Functions to Manage Directories, Files and Images
SOAP Extension Function and Calling Web Services
SOAP Server Functions and Examples
Localization Overview of Web Applications
Using Non-ASCII Characters in HTML Documents
Using Non-ASCII Characters as PHP Script String Literals
Receiving Non-ASCII Characters from Input Forms
"mbstring" Extension and Non-ASCII Encoding Management
Managing Non-ASCII Character Strings with MySQL Servers
Parsing and Managing HTML Documents
Configuring and Sending Out Emails
Managing PHP Engine and Modules on macOS