首页 > Web开发 > 详细

php动态获取函数参数

时间:2015-09-17 19:28:17      阅读:231      评论:0      收藏:0      [点我收藏+]
PHP 在用户自定义函数中支持可变数量的参数列表。其实很简单,只需使用 func_num_args() , func_get_arg() ,和 func_get_args()  函数即可。
可变参数并不需要特别的语法,参数列表仍按函数定义的方式传递给函数,并按通常的方式使用这些参数。 
 
1.func_num_args — 返回传入函数的参数总个数
int func_num_args ( void )
示例
<?php
function  demo ()
{
    $numargs  =  func_num_args ();
    echo  "参数个数为:  $numargs \n" ;
}
demo ( ‘a‘ ,  ‘b‘ ,  ‘c‘ );
运行结果
参数个数为: 3
 
2.func_get_args — 返回传入函数的参数列表
array func_get_args  ( void )
示例
<?php
function  demo ()
{
    $args = func_get_args();
    echo "传入的参数分别为:";
    var_dump($args);
}
demo ( ‘a‘ ,  ‘b‘ ,  ‘c‘ );

运行结果

传入的参数分别为:
array (size=3)
  0 => string ‘a‘ (length=1)
  1 => string ‘b‘ (length=1)
  2 => string ‘c‘ (length=1)
 
3.func_get_arg — 根据参数索引从参数列表返回参数值
mixed  func_get_arg  ( int $arg_num  )
示例
<?php
function  demo ()
{
    $numargs  =  func_num_args ();
    echo  "参数个数为:  $numargs <br />" ;
    $args = func_get_args();
    if ( $numargs  >=  2 ) {
        echo  "第二个参数为: "  .  func_get_arg ( 1 ) .  "<br />" ;
    }
}
demo ( ‘a‘ ,  ‘b‘ ,  ‘c‘ );
运行结果
参数个数为: 3
第二个参数为: b

 

php动态获取函数参数

原文:http://www.cnblogs.com/chenqionghe/p/4817275.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!