Jump to content

func_get_args() explanation


SchweppesAle

Recommended Posts

hi, I'm doing a little reading up.  The following example was presented on how to use func_get_args()

 

function countAll($arg1)
{
  if (func_num_args() == 0) {
    die("You need to specify at least one argument");
  } else {
    $args = func_get_args(); // Returns an array of arguments
    // Remove the defined argument from the beginning
    array_shift($args);
    $count = strlen ($arg1);
    foreach ($args as $arg) {
      $count += strlen($arg);
    }
  }
  return $count;
}
echo countAll("foo", "bar", "baz"); // Displays ’9’

 

The part which I don't understand is array_shift($args).  Wouldn't that pop "foo" off the array created by $args = func_get_args()?  After that there would be 6 characters instead of 9.  What's being popped off?  :shrug:

Link to comment
https://forums.phpfreaks.com/topic/187508-func_get_args-explanation/
Share on other sites

its a bit confusing, but you should really use a debugger to find out what's happening.

all the similar names confused me, but here it goes

// function countall('foo')
function countAll($arg1)
{
  // if(3==0)
  if (func_num_args() == 0) {
    die("You need to specify at least one argument");
  } else {
    // $args=array('foo','bar','baz');
    $args = func_get_args(); // Returns an array of arguments
    // Remove the defined argument from the beginning
   // $args=array('bar','baz');
    array_shift($args);
    // $count=strlen('foo'); taken from function arguments, not get_func_args
    $count = strlen ($arg1);
    foreach ($args as $arg) {
      $count += strlen($arg);
    }
  }
  return $count;
}
echo countAll("foo", "bar", "baz"); // Displays ’9’

The part which I don't understand is array_shift($args).  Wouldn't that pop "foo" off the array created by $args = func_get_args()?  After that there would be 6 characters instead of 9.  What's being popped off?  :shrug:

 

It does remove the "foo" value from the array so the foreach loop will only loop over the remaining two values ("bar" and "baz"). Note, that the $counter is given the length of $arg1 (which is "foo") and then the two array value lengths are added.

In this function you explicitly define one argument $arg1.

That's why array_shift is used, to remove this argument from array returned by func_get_args()

 

Then it just adds the length of this first argument and the lengths of all other arguments.

wait wait wait.  I think I see it.

 

function countAll($arg1)
{
  if (func_num_args() == 0) {
    die("You need to specify at least one argument");
  } else {
    $args = func_get_args(); // Returns an array of arguments
    // Remove the defined argument from the beginning
    array_shift($args);
    $count = strlen ($arg1);
    foreach ($args as $arg) {
      $count += strlen($arg);
    }
  }
  return $count;
}
echo countAll("foo", "bar", "baz"); // Displays ’9’

 

foo is passed into the args1 parameter.  So we have 3 arguments along with bar and baz.  $count = strlen($arg1).  THEN 1 of the 3 arguments is popped off the array($args).  The foreach cycles through the two remaining array elements in $arg and adds the resulting number of characters to $count.  Didn't notice that before

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.