Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.