Home >Backend Development >PHP Tutorial >How to define a function with variable number of parameters in PHP

How to define a function with variable number of parameters in PHP

怪我咯
怪我咯Original
2017-06-28 09:23:511052browse

This article mainly introduces the phpdefine the function usage with variable number of parameters. It analyzes the usage skills of func_get_args with examples. It has certain reference value. Friends who need it can refer to it. Next

The example in this article describes the usage of functions with variable number of parameters defined in PHP. Share it with everyone for your reference. The specific analysis is as follows: The

function parameters in

php can not be fixed, or even need to be defined. Use the func_get_args() function inside the function to obtain the parameter list. You can specify any for the function when calling. Parameters, very convenient

<?php
 function addanything (){
  $total = 0;
  $args = func_get_args ();
  for ($i = 0; $i < count ($args); $i++){
   if (is_int ($args[$i])){
    $total += $args[$i];
   }
  }
  return $total;
 }
 echo addanything (1,5,7,8,11) . "<br />";
?>

The above is the detailed content of How to define a function with variable number of parameters in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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