PHP DevCenter
oreilly.comSafari Books Online.Conferences.

advertisement


PHP Foundations

Advanced PHP Variables and Functions

05/17/2001

Today we'll discuss an advanced function concept called variable-length argument lists, and explain how to use dynamic function and variable names in PHP.

Variable-length argument lists

Until now, we have looked at parameters that are passed to custom functions to be statically defined (meaning once you decide on a set of parameters you are stuck with them until you change the declaration). Today, we'll discuss how you can create functions that define no parameters yet still function as expected when any number of parameters are passed to them. This concept of variable-length argument lists is made possible by a small family of built-in PHP functions: func_num_args(), func_get_arg(), and func_get_args().

The func_* function family

The func_* family of functions is a set of functions designed to run inside another function. Their purpose is to retrieve information regarding the arguments passed to the function in question. Because PHP allows you to pass parameters to functions that take no parameters in their definition, these functions can be used to create functions that take an unlimited number of parameters. For instance, consider the following snippet of code:

<?php
  function dynamic_args() {

  for($i = 0 ; $i < func_num_args(); $i++) {

  echo "Argument $i = ".func_get_arg($i)."<br />";

  }
  }

  dynamic_args("a", "b", "c", "d", "e");
?>

Compared to earlier examples of functions, the example above seems incorrect because we are defining a function that takes no parameters yet we are calling the function and passing five parameters to it. As stated earlier, PHP will allow us to pass extra parameters to our functions without error -- our real interest is in the function itself.

In the above function, we are using two of the func_* family commands to gather information regarding the parameters passed to dynamic_args(). First, we use func_num_args() to return the total number of arguments passed to dynamic_args(). In this case, the total number of parameters passed was five. Then, from within the for loop, we use the function func_get_args() and pass the index $i to it. When this is done, func_get_args() returns with the value of the parameter which equates to the index provided. This process continues until our for loop ends. The results of calling dynamic_args() with the parameters we specified would be the following output to the browser:

Argument 0 = a
Argument 1 = b
Argument 2 = c
Argument 3 = d
Argument 4 = e

As you can see, through the use of these very powerful functions available to us, we are able to create functions that can accept an unlimited number of parameters with ease. We can also use this method to create functions that require one parameter followed by an unlimited number of additional parameters as shown below:

<?php
  function implode_str($glue) {
    $string = "";

    for($i = 1; $i < func_num_args(); $i++) {
      $string .= $glue;
      $string .= func_get_arg($i);
    }
			
    return $string;

  }
	
  echo implode_str('-', 'This', 'is', 'a', 'test');
?>

In the above example, we define a function implode_str() that requires a single parameter $glue. Then, inside of our function we use the same function calls as we did in our earlier example to create a string $string based on the value of the extra arguments passed and the value of the $glue variable. Note that our for loop now starts off at 1 rather than 0. This is because we are declaring a single variable in our definition, so we will ignore it by only looking at variables passed after it that parameter. When the function is executed with the parameters above, its output will be -This-is-a-test. This is just one of the many ways to mix and match function definitions with the func_* family of functions to create variable-length argument lists.

Pages: 1, 2, 3

Next Pagearrow




Valuable Online Certification Training

Online Certification for Your Career
Earn a Certificate for Professional Development from the University of Illinois Office of Continuing Education upon completion of each online certificate program.

PHP/SQL Programming Certificate — The PHP/SQL Programming Certificate series is comprised of four courses covering beginning to advanced PHP programming, beginning to advanced database programming using the SQL language, database theory, and integrated Web 2.0 programming using PHP and SQL on the Unix/Linux mySQL platform.

Enroll today!


Sponsored by: