Variable functions

PHP supports the concept of variable functions. This means that if a variable name has parentheses appended to it, PHP will look for a function with the same name as whatever the variable evaluates to, and will attempt to execute it. Among other things, this can be used to implement callbacks, function tables, and so forth.

Example 12-1. Variable function example

  1 
  2 <?php
  3 function foo() {
  4     echo "In foo()<br>\n";
  5 }
  6 
  7 function bar( $arg = '' ) {
  8     echo "In bar(); argument was '$arg'.<br>\n";
  9 }
 10 
 11 $func = 'foo';
 12 $func();
 13 $func = 'bar';
 14 $func( 'test' );
 15 ?>
 16