TUTORIAL: Pass Variable Num. of Arguments to a PHP Function

  • spork
  • Brewmaster
  • Silver Member
  • User avatar
  • Posts: 6249
  • Loc: Seattle, WA

Post 3+ Months Ago

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:

Code: [ Select ]
<?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";
?>
}
  1. <?php
  2. function sum() {
  3.  
  4.     $num = func_num_args();
  5.  
  6.     $total = 0;
  7.     for( $i = 0; $i < $num; ++$i ) {
  8.         $total += func_get_arg($i);
  9.     }
  10.  
  11.     return $total;
  12. }
  13.  
  14. $total = sum(1, 2, 3, 4, 5);
  15. echo "Total: $total";
  16. ?>
  17. }


How It Works


Let's look at the code line by line:

Code: [ Select ]
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.

Code: [ Select ]
$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.

Code: [ Select ]
$total = 0;
for( $i = 0; $i < $num; ++$i ) {
    $total += func_get_arg($i);
}
  1. $total = 0;
  2. for( $i = 0; $i < $num; ++$i ) {
  3.     $total += func_get_arg($i);
  4. }

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.

Code: [ Select ]
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:
Code: [ Select ]
<?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";
?>
  1. <?php
  2. function sum() {
  3.  
  4.     $args = func_get_args();
  5.     $num = func_num_args();
  6.  
  7.     $total = 0;
  8.     for( $i = 0; $i < $num; ++$i ) {
  9.         $total += $args[$i];
  10.     }
  11.  
  12.     return $total;
  13. }
  14.  
  15. $total = sum(1, 2, 3, 4, 5);
  16. echo "Total: $total";
  17. ?>


The other method involves passing arguments to a function as a self-contained array or associative array:
Code: [ Select ]
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";
?>
  1. function sum( $args ) {
  2.  
  3.     if( !is_array($args) || is_empty($args) ) {
  4.         return 0;
  5.     }
  6.  
  7.     $num = sizeof($args);
  8.  
  9.     $total = 0;
  10.     for( $i = 0; $i < $num; ++$i ) {
  11.         $total += $args[$i];
  12.     }
  13.  
  14.     return $total;
  15. }
  16. $total = sum(array(1, 2, 3, 4, 5));
  17. echo "Total: $total";
  18. ?>


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!
Attachments:
MultipleArgumentsPHP.zip

(465 Bytes) Downloaded 402 times

Complete source code for the sum() example used in this tutorial.

  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

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.

Post Information

  • Total Posts in this topic: 2 posts
  • Moderator: Tutorial Writers
  • Users browsing this forum: No registered users and 2 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.