Usage example of defining a function with variable number of parameters in PHP_PHP Tutorial

WBOY
Release: 2016-07-13 10:03:41
Original
803 people have browsed it

Usage examples of functions with variable number of parameters defined in PHP

This article mainly introduces the usage of functions with variable number of parameters defined in PHP. The examples analyze the usage skills of func_get_args, with Be sure to refer to the reference value. Friends who need it can refer to it

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 do not need to be fixed, and there is no need to even define parameters. Use the func_get_args() function inside the function to obtain the parameter list. You can specify any parameters for the function when calling, which is very convenient

?

1

2

3

4

5

6

7

8

9

10

11

12

13

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) . "
";

?>

1 2

3

4 5

67 8 9 10 11 12
13
<🎜>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) . "
"; ?>
I hope this article will be helpful to everyone’s PHP programming design. http://www.bkjia.com/PHPjc/968661.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/968661.htmlTechArticlePHP defines a function with a variable number of parameters. This article mainly introduces PHP to define a function with a variable number of parameters. Usage, examples analyze the usage skills of func_get_args, with certain reference...
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