Detailed explanation of usage examples of variable parameters of PHP user-defined functions

伊谢尔伦
Release: 2023-03-11 12:58:02
Original
1357 people have browsed it

php supports variable number of parameter lists in user custom functions.

In php5.5 and earlier versions, use the func_num_args(), func_get_arg(), func_get_args() function implementation.

Copy after login

Output:

1
a
Array
(
  [0] => a
)

3
1
Array
(
  [0] => 1
  [1] => 2
  [2] => 3
)

2
Array
(
  [0] => d
  [1] => e
)

Array
(
  [0] => Array
    (
      [0] => d
      [1] => e
    )

  [1] => Array
    (
      [0] => f
    )

)
Copy after login

In php5.6 and above, you can use... syntax.

Example 1: Use...$args to replace any number of parameters

Copy after login

The output results are the same as php5.5 using func_num_args(), func_get_arg(), func_get_args( ) function is consistent.

Example 2: ArrayConvert to parameter list

Copy after login

Example 3: Some parameters are specified, and the number of other parameters is variable

Copy after login

Output:

name:fdipzone
tag:programmer
args:
Array
(
)

name:terry
tag:designer
args:
Array
(
  [0] => 1
  [1] => 2
)

name:aoao
tag:tester
args:
Array
(
  [0] => Array
    (
      [0] => a
      [1] => b
    )

  [1] => Array
    (
      [0] => c
    )

  [2] => Array
    (
      [0] => d
    )

)
Copy after login

The above is the detailed content of Detailed explanation of usage examples of variable parameters of PHP user-defined functions. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!