当前位置:Gxlcms > PHP教程 > PHP函数处理函数实例详解

PHP函数处理函数实例详解

时间:2021-07-01 10:21:17 帮助过:3人阅读

1. call_user_func和call_user_func_array:

以上两个函数以不同的参数形式调用回调函数。见如下示例:

   

运行结果如下:

bogon:TestPhp$ php call_user_func_test.php This is Test2::printSelf.This is Test2::doSomething.This is Test2::doSomethingWithArgs with ($arg1 = hello and $arg2 = world).This is Test2::doSomethingWithArgs with ($arg1 = hello2 and $arg2 = world2).

2. func_get_args、func_num_args和func_get_args:

这三个函数的共同特征是都很自定义函数参数相关,而且均只能在函数内使用,比较适用于可变参数的自定义函数。他们的函数原型和简要说明如下:
int func_num_args (void) 获取当前函数的参数数量。
array func_get_args (void) 以数组的形式返回当前函数的所有参数。
mixed func_get_arg (int $arg_num) 返回当前函数指定位置的参数,其中0表示第一个参数。

 $arg) {        print "$arg\n";    }}myTest('hello','world','123456');

运行结果如下:

Stephens-Air:TestPhp$ php class_exist_test.php The number of args in myTest is 3The 0th arg is helloThe 1th arg is worldThe 2th arg is 123456-------------------------------------------helloworld123456

3. function_exists和register_shutdown_function:

函数原型和简要说明如下:

bool function_exists (string $function_name) 判断某个函数是否存在。
void register_shutdown_function (callable $callback [, mixed $parameter [, mixed $... ]]) 在脚本结束之前或者是中途调用exit时调用改注册的函数。另外,如果注册多个函数,那么他们将会按照注册时的顺序被依次执行,如果其中某个回调函数内部调用了exit(),那么脚本将立刻退出,剩余的回调均不会被执行。

   

运行结果如下:

Stephens-Air:TestPhp$ php call_user_func_test.php shutdown function exists now.This test is executed.$arg1 = Hello, $arg2 = World

人气教程排行