PHP Function Argument Lists

June 21, 2007

A lot of PHP developer’s don’t know about PHP’s support for variable-length argument lists.

This is a quite an easy feature to use and can be useful when you’re not sure how many arguments a function should accept.

To declare a function with a variable-length argument list, declare it with an empty argument list.

function add_scores()

Calling the function with multiple arguments is easy…

add_scores(88, 37, 99, 32);

Processing the arguments inside the function is also an easy task using the func_get_arg(), func_get_args() and func_num_args() functions.

function add_scores()
{
return array_sum(func_get_args());
}

Output: 256

Leave a Comment

Previous post:

Next post: