Monday, September 29, 2008

func_num_args, func_get_args, func_num_args in PHP

func_num_args
-------------


func_num_args — Return an item from the argument list

Example:

function allthephp_func_num_args()
{
$numargs = func_num_args();
echo "Number of arguments: $numargs\n";
}

allthephp_func_num_args(1, 2, 3);

?>

Output:

Number of arguments: 3


func_get_arg
-------------


func_get_arg — Return an item from the argument list


Description:

Gets the specified argument from a user-defined function's argument list.
This function may be used in conjunction with func_get_args() and func_num_args() to allow
user-defined functions to accept variable-length argument lists.


Example:

function allthephp_func_num_arg()
{
$numargs = func_num_args();
echo "Number of arguments: $numargs\n";
if ($numargs >= 2) {
echo "Second argument is: " . func_get_arg(1) . "\n";
}
}

allthephp_func_num_arg (-1, 20, 13);
?>

Output:

Number of arguments: 3
Second argument is: 20


func_get_args
-------------


func_get_args — Returns an array comprising a function's argument list


Description:

Gets an array of the function's argument list.
This function may be used in conjunction with func_get_arg() and func_num_args() to allow
user-defined functions to accept variable-length argument lists.


Example:

function allthephp_func_num_args()
{
$numargs = func_num_args();
echo "Number of arguments: $numargs\n";
if ($numargs >= 2) {
echo "Second argument is: " . func_get_arg(1) . "\n";
}

$arg_list = func_get_args();
for ($i = 0; $i < $numargs; $i++) {
echo "Argument $i is: " . $arg_list[$i] . "\n";
}

}

allthephp_func_num_args (-1, 20, 13);
?>

Output:

Number of arguments: 3
Second argument is: 20
Argument 0 is: -1
Argument 1 is: 20
Argument 2 is: 13

No comments: