PHP variable-length parameter processing function Example detailed

Source: Internet
Author: User
This article mainly introduces the PHP variable length parameter processing function, combined with the instance form Analysis Func_num_args (), Func_get_arg () and Func_get_args () three variable long parameter processing function function and the use method, the need friend can refer to the next

The example of PHP variable length parameter processing function is described in this paper. Share to everyone for your reference, as follows:

Like C + +, a function that contains mutable parameters can be processed in PHP, just as it is, and requires a function to handle a list of parameters specifically. Three related functions are available in PHP:

1. Func_num_args ()--Returns the number of arguments passed to the function

int func_num_args ( void )

Example:

function Open_database ($DB, $cache _size_or_values=null, $cache _size=null) {  switch (Function_num_args ())  { Case    1:      $r = select_db ($DB);      break;    Case 2:      $r = select_db ($DB, $cache _size_or_values);      break;    Case 3:      $r = select_db ($DB, $cache _size_or_values, $cache _size);      break;  }  Return Is_resource ($R);}

2. Func_get_arg ()--Returns the specified parameter

mixed func_get_arg ( int arg_num)

The arg_num of the first parameter in the argument list is 0

<?php  function Some_func ($a, $b)  {for    ($i = 0; $i <func_num_args (); + + $i)    {      $param = Func_get _arg ($i);      echo "The param is $param \ n"    }  }  Some_func (1,3,5,7,9);? >

3. Func_get_args ()--Returns the parameter list as an array

array func_get_args ( void )

Example:

<?php  function Some_other_func ($a, $b)  {    $param = Func_get_args ();    $param = Join (",", $param);  Decomposition shows    echo "The arglist is: $param \ n";  }  Some_other_func (1,3,5,7,9);? >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.