PHP – get function arguments examples

PHP func_get_args(), func_get_arg() and func_num_args() can be used to retrieve php function arguments inside a function. This may be useful in debugging. Here are code snippets for it.

func_get_args() example

<?php
function f1() {
  $num = func_num_args();
  $args = func_get_args();
  print "num=$num\n";
  print "====args====\n";
  print_r($args);
}
f1("v1", "v2", "v3");
?>
num=3
====args====
Array
(
    [0] => v1
    [1] => v2
    [2] => v3
)
Env: PHP 5.5.9 (Linux)

func_get_args() and default value

In case a function argument has default value, func_get_args() is not impacted by it.

<?php
function f1($var1, $var2='some default') {
  $num = func_num_args();
  $args = func_get_args();
  print "num=$num\n";
  print "====args====\n";
  print_r($args);
}
f1("v1");
num=1
====args====
Array
(
    [0] => v1
)
Env: PHP 5.5.9 (Linux)

Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit

Comments

Click here to write/view comments