Introduction
Note: The complete source code for the example in this tutorial can be downloaded at the end of the tutorial.Note: Trying to do this in C? Check this out.When declaring a function, the type and number of arguments are usually fixed at compile time. But sometimes it is necessary for a function to be able to accept a variable number of arguments unknown until run-time.
This tutorial will show you how to create a PHP function that can accept a variable number of arguments.
In A Nutshell
If you're simply looking for an example to go off of, I won't keep you waiting. Here's an example of a function sum() that adds up all of the integers passed to it and returns the sum:
<?php
function sum() {
$num = func_num_args();
$total = 0;
for( $i = 0; $i < $num; ++$i ) {
$total += func_get_arg($i);
}
return $total;
}
$total = sum(1, 2, 3, 4, 5);
echo "Total: $total";
?>
}
- <?php
- function sum() {
-
- $num = func_num_args();
-
- $total = 0;
- for( $i = 0; $i < $num; ++$i ) {
- $total += func_get_arg($i);
- }
-
- return $total;
- }
-
- $total = sum(1, 2, 3, 4, 5);
- echo "Total: $total";
- ?>
- }
How It Works
Let's look at the code line by line:
We declare our function. Notice that we do not need to do anything special here, just declare the function as if it were a normal function with no parameters.
func_num_args is a PHP function that returns the number of arguments that were passed into the function. Here, we store this value in a variable named
$num.
$total = 0;
for( $i = 0; $i < $num; ++$i ) {
$total += func_get_arg($i);
}
- $total = 0;
- for( $i = 0; $i < $num; ++$i ) {
- $total += func_get_arg($i);
- }
Now loop through the list of arguments and add their values to the total.
func_get_arg is a PHP function the retrieves the value of the specified by the index passed to it.
Finally, we return our total.
A Few Alternatives
There are a couple of other ways to manage variable-length argument lists in PHP.
The first method involves using the PHP function
func_get_args. This function will retrieve the entire argument list and return it as an array:
<?php
function sum() {
$args = func_get_args();
$num = func_num_args();
$total = 0;
for( $i = 0; $i < $num; ++$i ) {
$total += $args[$i];
}
return $total;
}
$total = sum(1, 2, 3, 4, 5);
echo "Total: $total";
?>
- <?php
- function sum() {
-
- $args = func_get_args();
- $num = func_num_args();
-
- $total = 0;
- for( $i = 0; $i < $num; ++$i ) {
- $total += $args[$i];
- }
-
- return $total;
- }
-
- $total = sum(1, 2, 3, 4, 5);
- echo "Total: $total";
- ?>
The other method involves passing arguments to a function as a self-contained array or associative array:
function sum( $args ) {
if( !is_array($args) || is_empty($args) ) {
return 0;
}
$num = sizeof($args);
$total = 0;
for( $i = 0; $i < $num; ++$i ) {
$total += $args[$i];
}
return $total;
}
$total = sum(array(1, 2, 3, 4, 5));
echo "Total: $total";
?>
- function sum( $args ) {
-
- if( !is_array($args) || is_empty($args) ) {
- return 0;
- }
-
- $num = sizeof($args);
-
- $total = 0;
- for( $i = 0; $i < $num; ++$i ) {
- $total += $args[$i];
- }
-
- return $total;
- }
- $total = sum(array(1, 2, 3, 4, 5));
- echo "Total: $total";
- ?>
Conclusion
You should now know how to create a PHP function that accepts a variable number of arguments at run-time. This technique is particularly useful when creating wrapper functions for functions that already accept a variable number of arguments, such as printf(). This is demonstrated in my other tutorial,
Writing a Custom printf() Wrapper Function in PHP.
I always welcome questions or feedback about this tutorial. Simply post a reply or PM me; I'm glad to help!
MultipleArgumentsPHP.zip
(465 Bytes) Downloaded 375 times
Complete source code for the sum() example used in this tutorial.