Home  >  Article  >  Backend Development  >  How to solve the problem of uncertain number of parameters in PHP

How to solve the problem of uncertain number of parameters in PHP

藏色散人
藏色散人Original
2020-08-18 10:44:193145browse

php solution to the uncertain number of parameters: 1. Return the number of actual parameters through the "func_num_args" function; 2. Return a certain actual parameter through the "func_get_arg" function; 3. Return using the "func_get_args" function An array of actual parameters.

How to solve the problem of uncertain number of parameters in PHP

Recommended: "PHP Video Tutorial"

Solution to the problem of uncertain number of function parameters in php

func_num_args: Returns the number of actual parameters;

func_get_arg: Returns a certain actual parameter, and the index of an actual parameter must be passed in (that is, the index of the parameter at this time in the array returned by func_num_args Index);

func_get_args: Returns an array of actual parameters;

chestnut

function add(){
$args = func_get_args()
print_r($args);
}

1. Disadvantages When we write a function without defining an actual parameter, sometimes the caller does not I am confused about what parameters to pass

2. It is recommended to use...$args

function add(…$args){
print_r($args);
}

Note,

1. When several parameters are passed into add(), the function The internal $args is an array,

The function is called internally...$args will be parsed into function parameters

2. It is a...[ ]The internal $args is a through Go in...The following array

...$args is used to combine parameters to form an array. When calling a function...$args is used to expand the array into function parameters. It's exactly the opposite process.

The above is the detailed content of How to solve the problem of uncertain 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