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";
?>
}

How It Works

Let's look at the code line by line:

function sum() {

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.

$num = func_num_args();

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);
}

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.

return $total;

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";
?>

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";
?>

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! Full source:

<?php

/*
 * A function that accepts a variable number of
 * arguments at runtime.
 *
 * @author Jeff Linse
 * Created for the tutorial found at
 * https://www.ozzu.com/wiki/tagged/php
 */

// declare the function with no parameters
function sum() {

	// get the number of arguments passed in
	$num = func_num_args();

	// loop through and sum up the numbers
	$total = 0;
	for( $i = 0; $i < $num; ++$i ) {

		// get the next argument in the list
		$total += func_get_arg($i);
	}

	// return the total
	return $total;
}

// sum up some integers
$total = sum(1, 2, 3, 4, 5);
echo "Total: $total";

?>

This page was published on It was last revised on

0

1 Comment

  • Votes
  • Oldest
  • Latest
JO
184 4
Commented
Updated

An added tidbit that's nice to know is that func_get_args is available within class methods too, unlike how the method_exists and function_exists methods are designed for classes-vs-functions.

Classes actually don't even have any sort of "method_get_args" counterpart.

add a comment
0