Before starting this article, I don’t know how much you know about PHP functions. If you are still a newbie, I recommend that you read the "PHP Function" chapter in the PHP self-study manual. After all, There is a saying that the real power of PHP comes from its functions~
So this article will introduce you to the basic knowledge about functions: calculating the number of parameters passed in the PHP function.
First of all, I will briefly introduce how to create a function:
is as follows:
<?php function functionName($x,$y) { // 要执行的代码 }
In PHP, you can create a function through the function keyword. "functionName" represents the custom function name; ($x,$y) represents the passed parameters. Two parameters $x and $y are clearly defined here; {...} is the function code to be executed. .
After briefly introducing the creation of functions, we will go straight to the topic!
How to count the number of parameters passed in a PHP function?
The answer is: In PHP, you can use the func_num_args()
function to count the number of parameters passed into the PHP function.
The sample code is as follows:
<?php function count_param() { $count_args = func_num_args(); echo "参数的数量: $count_args\n"; } count_param(10, 20); // 参数个数: 2
Output:
参数的数量: 2
In the above code, it can be clearly seen that the parameters we pass to the function are (10, 20) That is, 2 parameters, and the result of calculating the number of parameters is also 2.
Here you need to understand the func_num_args()
function. The introduction to the func_num_args()
function is as follows:
func_num_args()
The function is used to return the number of parameters passed to the function. Its syntax is "func_num_args(): int";
The return value is: Returns the number of parameters passed into the current user-defined function .
This function can be used with func_get_arg() and func_get_args() to allow user-defined functions to accept variable-length argument lists.
Note: If called from outside a user-defined function, a warning is generated.
Finally, I would like to recommend the latest and most comprehensive "PHP Video Tutorial"~ Come and learn!
The above is the detailed content of How to count number of parameters passed in PHP function. For more information, please follow other related articles on the PHP Chinese website!