Team LiB
Previous Section Next Section

Dynamic Variables and Functions

Dynamic Variables

More than for just manipulating data, PHP enables you to create a variable whose actual identifier (just as $foo is an identifier) is unknown until the script is executed. This concept of "variable variables," although not used in day-to-day development, is invaluable in certain circumstances, as you will see when we discuss forms later in the book. The syntax used when you would like to reference a particular value as the name of a variable is as follows:

${<expression>}

<expression> can represent any valid PHP expression that evaluates to a value that follows the rules outlined earlier in this chapter regarding variable names. Consider the following two lines of code that follow, each of which manipulates the variable $foo:

<?php

    $foo = 5;
    ${"foo"}++; // the $foo variable now equals 6
    $my_var_name = "foo";
    ${$my_var_name}++;

?>

What happens when the last line of the preceding snippet is executed? If you were to say it set the variable $foo to 7, you would be correct! Looking at the preceding code, you can see that the $my_var_name variable represents the string "foo". When the next line of PHP is executed, $my_var_name is evaluated and the result is then used as the name of the variable. Hence, the $foo variable is incremented.

Dynamic Functions

Along with dynamic variables, PHP can also execute a function dynamically. This is particularly useful when validating form data, as you will learn later in the book, and it is used very easily. To execute a function whose name you do not know until runtime, simply append a parameter list to the end of any variable. Consider the following code snippet:

<?php

    function test() {
        echo "Hello, PHP!<BR>";
    }

    $myfunc = "test";
    $myfunc();

?>

When the preceding code is executed, as you might expect the result will be "Hello, PHP!" to the client. Although in this case the test() function accepted no parameters and returned no value, this would have also been possible when calling the function dynamically.

While on the subject of function parameters, let's take a look at the concept of dynamic function parameters. Thus far, I have shown you only functions that accept a predetermined number of parameters. However, PHP also supports the capability to pass parameters to a given function dynamically without defining them prior to the function's execution.

To accomplish this, let's look at two PHP functions: func_num_args() and func_get_args(). Neither function takes any parameters and are to be used only from within a PHP function. As the names imply, func_num_args() returns the total number of arguments that were passed to the current function. To complement this function, func_get_args() is used to return an indexed array containing the values for each parameter that was passed. Following is an example of both in use to create a function that can accept an undetermined number of parameters:

<?php

    function dynamic_func() {
        echo "There are: ".func_num_args()." Arguments passed.<BR>";
        $args = func_get_args();

        for($i = 0; $i < count($args); $i++) {
            echo "Passed Value: {$args[$i]}<BR>";
        }

      }

    dynamic_func(1,2,3,4,5);

?>

    Team LiB
    Previous Section Next Section