How to use PHP function to pass variable parameters

藏色散人
Release: 2023-03-03 14:16:02
Original
3655 people have browsed it

In PHP, you can use the "func_get_args()" function to pass an indefinite number of parameters. The syntax of this function is "func_get_args (void): array". The return value is to return an array, in which each element is A copy of the corresponding element of the current user-defined function's parameter list.

How to use PHP function to pass variable parameters

Recommended: "PHP Video Tutorial"

If you want to pass an indefinite number of parameters, you need to use func_get_args() Function to pass

func_num_args() function to return the total number of parameters

<?php
	function more_args(){
		$args = func_get_args();
		for($i=0;$i<func_num_args();$i++){
			$a = $i +1;
			echo "第".$a."个参数是".$args[$i]."<br>";
		}
	}
	more_args(&#39;a&#39;,&#39;b&#39;,&#39;c&#39;,&#39;d&#39;,&#39;e&#39;,&#39;f&#39;);
?>
Copy after login

Execution results

##Related introduction:

func_get_args — Returns an array containing the function parameter list

Description

func_get_args ( void ) : array
Copy after login
Gets the array of function parameter list.

This function can be used together with func_get_arg() and func_num_args(), so that the user-defined function can accept a custom number of parameter lists.

Return value

Returns an array, each element of which is a copy of the corresponding element of the parameter list of the current user-defined function.

The above is the detailed content of How to use PHP function to pass variable 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