php variable length arguments


/ Published in: PHP
Save to your folder(s)

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


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.