func_get_args和func_num_args详解

高洛峰
Libérer: 2023-03-05 15:40:01
original
1316 人浏览过

func_get_args()—返回的是一个数组,这个数组内的每一项都是函数的一个参数。根据php手册我们给出函数的用法格式。

array func_get_args ( void )
Copier après la connexion
Copier après la connexion

如果只是在这里笼统的阐述,可能大家不能够真正的了解这个函数,那么就让我们通过事例来看下这个函数的用法。

function foo() {     
 $args = func_get_args();   
 foreach ($args as $k => $v) {
  echo “arg”.($k+1).”: $v\n”;
  }  
}  
foo();  /* 没用任何输出*/  
foo(‘hello’);  /* 输出  arg1: hello  */ 
foo(‘hello’, ‘world’, ‘again’);  /*输出 arg1: hello  arg2: world  arg3: again  */
Copier après la connexion
Copier après la connexion

这个函数可以把你传入的所有参数全部都放在一个数组中,然后再输出。这样对我们以后编写php程序是不是又简单了许多呢?
既然说到了func_get_args函数,那么我们就不能不提下func_num_args函数和func_get_arg函数了

func_nums_args——统计传入函数参数的个数

func_get_arg——根据索引取得某一个参数,这里的索引数传入函数的参数

我们就以php手册上的例子来看吧

Copier après la connexion
Copier après la connexion

上面的例子很明白的给我们展示了func_num_args函数就是活的传入函数的参数

    \n";
         if ($numargs >= 2) {
         echo "Second argument is: " . func_get_arg(1) . "
\n";          }     }           foo (1, 2, 3);     //Prints     //Number of arguments: 3     //Second argument is: 2     ?>
Copier après la connexion
Copier après la connexion

上面的例子中func_get_arg(1)就是获取函数的第二个参数。好了,我们看下这三个函数的综合实例吧,这样我们就可以把这三个函数掌握了。

\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";     } } foo(1, 2, 3); /*Number of arguments: 3 Second argument is: 2 Argument 0 is: 1 Argument 1 is: 2 Argument 2 is: 3*/ ?>
Copier après la connexion
Copier après la connexion

func_get_args()—返回的是一个数组,这个数组内的每一项都是函数的一个参数。根据php手册我们给出函数的用法格式。

array func_get_args ( void )
Copier après la connexion
Copier après la connexion

如果只是在这里笼统的阐述,可能大家不能够真正的了解这个函数,那么就让我们通过事例来看下这个函数的用法。

function foo() {     
 $args = func_get_args();   
 foreach ($args as $k => $v) {
  echo “arg”.($k+1).”: $v\n”;
  }  
}  
foo();  /* 没用任何输出*/  
foo(‘hello’);  /* 输出  arg1: hello  */ 
foo(‘hello’, ‘world’, ‘again’);  /*输出 arg1: hello  arg2: world  arg3: again  */
Copier après la connexion
Copier après la connexion

这个函数可以把你传入的所有参数全部都放在一个数组中,然后再输出。这样对我们以后编写php程序是不是又简单了许多呢?
既然说到了func_get_args函数,那么我们就不能不提下func_num_args函数和func_get_arg函数了

func_nums_args——统计传入函数参数的个数

func_get_arg——根据索引取得某一个参数,这里的索引数传入函数的参数

我们就以php手册上的例子来看吧

Copier après la connexion
Copier après la connexion

上面的例子很明白的给我们展示了func_num_args函数就是活的传入函数的参数

    \n";
         if ($numargs >= 2) {
         echo "Second argument is: " . func_get_arg(1) . "
\n";          }     }           foo (1, 2, 3);     //Prints     //Number of arguments: 3     //Second argument is: 2     ?>
Copier après la connexion
Copier après la connexion

上面的例子中func_get_arg(1)就是获取函数的第二个参数。好了,我们看下这三个函数的综合实例吧,这样我们就可以把这三个函数掌握了。

\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";     } } foo(1, 2, 3); /*Number of arguments: 3 Second argument is: 2 Argument 0 is: 1 Argument 1 is: 2 Argument 2 is: 3*/ ?>
Copier après la connexion
Copier après la connexion

 更多func_get_args和func_num_args详解相关文章请关注PHP中文网!

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!