Advanced PHP Variables and Functions
Pages: 1, 2, 3
Variable variables and function names
Note: Variable functions and variables are a potential security risk if used improperly. Before using variable function calls or variables, it is important to consider possible security holes that could arise from malicious user input.
Beyond dynamic parameters, PHP also supports the ability to call functions dynamically, as well as use variables dynamically. That is, we can write scripts that call functions and access variables without knowing the names of the functions or variables until the script starts executing. We'll start with how this can be accomplished with variables first, and then conclude with a similar syntax to be used for executing functions.
Variable variables
It is possible to access a particular variable in PHP without even knowing its name until after the script starts executing. This is done through a special variable syntax that is defined as the following: ${<var name>} where the variable name is either a constant (such as "foo") or another variable containing a string representation of the name. Sound confusing? Why don't we look at some examples to help make things more clear:
<?php
$foo = "This is the variable foo<br />";
$bar = "This is the variable bar<br />";
$foobar = "This is the variable foobar<br />";
$foo_name = "foo";
$bar_name = "bar";
echo $foo;
echo $bar;
echo ${$foo_name};
echo ${$bar_name};
echo ${$foo_name.$bar_name};
?>
Above is what appears to be a fairly easy-to-understand script that simply echos different variables to the browser. However, the last three echo statements are referring to very strange looking variables. Consider the first of the last three echo statements. On this line, we are telling echo to print the variable whose name is contained within $foo_name. Because $foo_name's value is "foo", the echo statement will print the value of our PHP variable $foo to the browser. Although the second of the three is basically identical, the last statement is a little different. Here, we use the string concatenation operator '.' to combine the values of $foo_name and $bar_name to create the string "foobar" which we then use as the variable name for our last echo statement. The result of all of this is the following output:
This is the variable foo
This is the variable bar
This is the variable foo
This is the variable bar
This is the variable foobar
With this syntax, it is possible to write extremely complex scripts that are quite dynamic in nature with much less work then normally needed. Now that we have an idea on how to use variable variables, let's look at how to call functions based on the value of a variable.
Variable functions
Variable functions work on the same principal as variable variables. Basically given a variable containing a string representing a function in PHP, you can call the function simply by appending parentheses and any parameters to the end of the variable. For instance:
<?php
function myfunc($msg) {
echo $msg."<br />";
}
$func_name = "myfunc";
$func_name("Hello, this is a test");
?>
will call the function myfunc() and pass our message "Hello, this is a test" to it. We accomplish this task by calling the function through the use of the variable $func_name that contains the actual name of the function myfunc.