使用func_get_args()、func_num_args()、func_get_arg() 可以构造一个可变参数列表的函数。
首先大致介绍以上三个函数。
(1)array func_get_args ( void )
说明:函数传回一数组,数组的各个元素相当于是目前使用者定义函式的参数列的数目
(2)int func_num_args ( void )
说明 : 返回传递到目前定义函数的参数数目。如果是从函数定义的外面来呼叫此函数,则func_get_arg( )将会产生警告。
(3)mixed func_get_arg ( int $arg_num )
说明 :传回定义函数的参数列表的第arg_num个参数,其参数从0开始。且函数定义的外面来呼叫此函数会产生警告,且当arg_num大于函数实际传递的参数数目时亦会产生警告并返回FALSE。
示例:
<?php
/**
* 函数的多参数列表的实现
*
*/
function multiArgs()
{
/** 以数组的形式返回参数列表 */
$args = func_get_args();
/** 参数的个数 */
$args_num = func_num_args();
foreach ( $args as $key => $value )
{
echo 'This is '.($key+1).'th argument,'.$value.'<br/>';
}
echo 'Number of args is '.$args_num;
}
multiArgs(‘one’,'two’,'three’); /** output */
/**
This is 1th argument:one
This is 2th argument:two
This is 3th argument:three
Number of args is 3
*/
?>