PHP Variable Variables
Examples
Abdul Rahman Sherzad
Computer Science Lecturer
Herat University, Afghanistan
Definition
■ Variable variables take the value
of one variable, and treat that as
the name of another variable.
■ Variable variables are just
variables whose names can be
programmatically set and
accessed. Hence, they are also
called dynamic variable names.
2
Example 1
■ Employees share similar name. Hence, all
employees are given a unique identifier (ID, aka
code) by organizations. This unique code is
important for the organizations to distinguish their
employees.
■ Likewise, educational institutions assign a unique
code for their students.
■ This unique code is very important for the system,
but it alone is not very informative in reports.
■ Variable variables can be used in such situations to
cover both. Following code creates a variable called
$S20120, and outputs the string "Abdul Rahman
Sherzad".
3
Example 2
■ It is also possible to use value of a
variable/literal value as part of variable
variables names.
■ Of course, there are many other easy
ways to accomplish the functionality of
this program. But this is to explain
variable variables concept through
examples.
4
Example 2 - Array
■ This is another method to
accomplish Example 2 in the
previous slide using array.
5
Example 3
■ Variable variables can be used to
dynamically generate variables from an
associative array.
■ The PHP extract() built-in function also
takes an associative array and treats the
keys as variable names and the values
as variable values, (see next slide).
■ This example is to describe and
demonstrate the variable variables
applications, not reinventing the wheel.
6
Example 3 - Extract
■ The PHP extract() function takes an
associative array and treats the keys as
variable names and the values as
variable values.
■ The extract() has useful functionality
such as to prevent variables that already
exist.
■ Also it enables one to group the variables
by using prefixes too.
7
Example 4
■ Use variable variables to convert POST
variables to local variables, as illustrated
in the example.
■ This technique has several benefits.
– Lines of code are reduced,
– Central place to clean and sanitize
all external input,
– Easy change and modification of
variable names.
8
Example 5
■ PHP variable variables can simulate
Microsoft Excel’s indirect() function to
create dependent drop-down lists.
■ In this example, it is shown how to create
dependent drop-down lists, using
variable variables names.
■ Of course, there are better ways to
accomplish creating dependent drop-
down lists. In this presentation, the
purpose is to show the application and
example of variable variables in PHP.
9
Example 5 - complete
10
11

PHP Variable variables Examples

  • 1.
    PHP Variable Variables Examples AbdulRahman Sherzad Computer Science Lecturer Herat University, Afghanistan
  • 2.
    Definition ■ Variable variablestake the value of one variable, and treat that as the name of another variable. ■ Variable variables are just variables whose names can be programmatically set and accessed. Hence, they are also called dynamic variable names. 2
  • 3.
    Example 1 ■ Employeesshare similar name. Hence, all employees are given a unique identifier (ID, aka code) by organizations. This unique code is important for the organizations to distinguish their employees. ■ Likewise, educational institutions assign a unique code for their students. ■ This unique code is very important for the system, but it alone is not very informative in reports. ■ Variable variables can be used in such situations to cover both. Following code creates a variable called $S20120, and outputs the string "Abdul Rahman Sherzad". 3
  • 4.
    Example 2 ■ Itis also possible to use value of a variable/literal value as part of variable variables names. ■ Of course, there are many other easy ways to accomplish the functionality of this program. But this is to explain variable variables concept through examples. 4
  • 5.
    Example 2 -Array ■ This is another method to accomplish Example 2 in the previous slide using array. 5
  • 6.
    Example 3 ■ Variablevariables can be used to dynamically generate variables from an associative array. ■ The PHP extract() built-in function also takes an associative array and treats the keys as variable names and the values as variable values, (see next slide). ■ This example is to describe and demonstrate the variable variables applications, not reinventing the wheel. 6
  • 7.
    Example 3 -Extract ■ The PHP extract() function takes an associative array and treats the keys as variable names and the values as variable values. ■ The extract() has useful functionality such as to prevent variables that already exist. ■ Also it enables one to group the variables by using prefixes too. 7
  • 8.
    Example 4 ■ Usevariable variables to convert POST variables to local variables, as illustrated in the example. ■ This technique has several benefits. – Lines of code are reduced, – Central place to clean and sanitize all external input, – Easy change and modification of variable names. 8
  • 9.
    Example 5 ■ PHPvariable variables can simulate Microsoft Excel’s indirect() function to create dependent drop-down lists. ■ In this example, it is shown how to create dependent drop-down lists, using variable variables names. ■ Of course, there are better ways to accomplish creating dependent drop- down lists. In this presentation, the purpose is to show the application and example of variable variables in PHP. 9
  • 10.
    Example 5 -complete 10
  • 11.