Home > Backend Development > PHP Tutorial > PHP implements functions using a variable number of parameters

PHP implements functions using a variable number of parameters

墨辰丷
Release: 2023-03-27 16:02:02
Original
1159 people have browsed it

This article mainly introduces the method of using a variable number of parameters in PHP functions. Interested friends can refer to it. I hope it will be helpful to everyone.

php supports a variable number of parameter lists in user-defined functions.

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

<?php

function myfunc(){

  // 获取参数数量
  echo func_num_args().PHP_EOL;

  // 获取第一个参数的值:
  print_r(func_get_arg(0));
  echo PHP_EOL;

  // 获取所有参数的值
  print_r(func_get_args());
  echo PHP_EOL;

}

myfunc(&#39;a&#39;);
myfunc(1, 2, 3);
myfunc(array(&#39;d&#39;,&#39;e&#39;), array(&#39;f&#39;));

?>
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 version, you can use... syntax.

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

<?php

function myfunc(...$args){

  // 获取参数数量
  echo count($args).PHP_EOL;

  // 获取第一个参数的值:
  print_r($args[0]);
  echo PHP_EOL;

  // 获取所有参数的值
  print_r($args);
  echo PHP_EOL;

}

myfunc(&#39;a&#39;);
myfunc(1, 2, 3);
myfunc(array(&#39;d&#39;,&#39;e&#39;), array(&#39;f&#39;));

?>
Copy after login

The output result is the same as php5. 5 Use func_num_args(), func_get_arg(), func_get_args() functions consistently.

Example 2: Convert array to parameter list

<?php

function add($a, $b){
  echo $a + $b;
}

$args = array(1, 2);

add(...$args); // 输出3

?>
Copy after login

Example 3: Some parameters are specified, others The number of parameters is variable

<?php

function display($name, $tag, ...$args){
  echo &#39;name:&#39;.$name.PHP_EOL;
  echo &#39;tag:&#39;.$tag.PHP_EOL;
  echo &#39;args:&#39;.PHP_EOL;
  print_r($args);
  echo PHP_EOL;
}

display(&#39;fdipzone&#39;, &#39;programmer&#39;);
display(&#39;terry&#39;, &#39;designer&#39;, 1, 2);
display(&#39;aoao&#39;, &#39;tester&#39;, array(&#39;a&#39;,&#39;b&#39;), array(&#39;c&#39;), array(&#39;d&#39;));

?>
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 content of this article All content, I hope it will be helpful to everyone's study.


Related recommendations:

php ajax jquery implements click to load more content_jquery

PHP jQuery realizes dragging layers at will and instantly saving the drag position_jquery

PHP mysql Highcharts generates cake Status_jquery

The above is the detailed content of PHP implements functions using a variable number of parameters. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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