Posted By

silviud on 09/06/10


Tagged

php varargs


Versions (?)

php variable length arguments


 / Published in: PHP
 

allows for variable length arguments - something similar as python func(*args, **kw)

  1. function foo()
  2. {
  3. $numargs = func_num_args(); // number of arguments
  4.  
  5. if ($numargs >= 2) {
  6. echo "Second argument is: " . func_get_arg(1) . "<br />\n";
  7. }
  8. $arg_list = func_get_args(); // all arguments as an array
  9. for ($i = 0; $i < $numargs; $i++) {
  10. echo "Argument $i is: " . $arg_list[$i] . "<br />\n";
  11. }
  12. }
  13.  
  14. foo(1, 2, 3);
  15. foo(1, 2, 3, 4, 5);

Report this snippet  

You need to login to post a comment.