SchweppesAle Posted January 7, 2010 Share Posted January 7, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/187508-func_get_args-explanation/ Share on other sites More sharing options...
laffin Posted January 7, 2010 Share Posted January 7, 2010 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’ Quote Link to comment https://forums.phpfreaks.com/topic/187508-func_get_args-explanation/#findComment-990131 Share on other sites More sharing options...
SchweppesAle Posted January 7, 2010 Author Share Posted January 7, 2010 ? Quote Link to comment https://forums.phpfreaks.com/topic/187508-func_get_args-explanation/#findComment-990275 Share on other sites More sharing options...
salathe Posted January 7, 2010 Share Posted January 7, 2010 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? 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. Quote Link to comment https://forums.phpfreaks.com/topic/187508-func_get_args-explanation/#findComment-990280 Share on other sites More sharing options...
Mchl Posted January 7, 2010 Share Posted January 7, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/187508-func_get_args-explanation/#findComment-990283 Share on other sites More sharing options...
SchweppesAle Posted January 7, 2010 Author Share Posted January 7, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/187508-func_get_args-explanation/#findComment-990289 Share on other sites More sharing options...
salathe Posted January 7, 2010 Share Posted January 7, 2010 Didn't notice that before It's not a particularly good example of easy-to-understand code. Quote Link to comment https://forums.phpfreaks.com/topic/187508-func_get_args-explanation/#findComment-990292 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.