Posts Tagged ‘func_get_args’

php func_get_args

editphp / programmingcommentsNo Comments »
September 22nd 2009

Recently looking in Drupal’s source code I found the func_get_args function. It is quite a powerful function for newer version of php

It basically takes in a random number of arguments and parses them in an argument array.

Basically, here’s what how it works

function foo_bar() {

$args = func_get_args;

for($i = 0; $i< count($args; $i++)
{
       echo $args[$i] . '< br/ > ';
}

foo_bar('monday', 'tuesday', 'wednesday');

The above function call will return

monday
tuesday
wednesday

It works very much like passing in an array as a parameter except it's less clunky.

function foo_bar($args = array())
{
     for($i = 0; $i < count($args); $i++)
     {
          echo $args[$i] . '< br/ > ';
     }

}

foo_bar(array('Monday', 'Tuesday', 'Wednesday'));