Php variable length parameter processing function details, php Variable Function details

Source: Internet
Author: User

Php variable length parameter processing function details, php Variable Function details

This example describes the php variable length parameter processing function. We will share this with you for your reference. The details are as follows:

Like C ++, PHP can also process functions that contain variable parameters. The truth is the same. A function is required to process the parameter list. PHP provides three related functions:

1. func_num_args () -- returns the number of parameters 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 () -- return the specified parameter

mixed func_get_arg ( int arg_num)

The arg_num of the first parameter in the parameter 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 ); // display 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.