Home >Backend Development >PHP Tutorial >What are php variable parameters?

What are php variable parameters?

怪我咯
怪我咯Original
2017-06-28 09:26:391611browse

Look at a piece of code first

function concatenate($transform, ...$strings) {
	$string = '';
	foreach($strings as $piece) {
		$string .= $piece;
	}
	return($transform($string));
}

echo concatenate("strtoupper", "I'd ", "like ",
	4 + 2, " apples");

When the function is defined, use... Operator to indicate that this is a variable parameter, if you pass If 2 or more parameters are provided, these parameters will be added to this array.

Argument Unpacking
This is a function that echoes the above function.

Variadic functions allow you to declare the incoming parameter array, and parameter unpacking allows you to pass an array to a function and automatically unpack it inside the function. The example is as follows:

$email[] = "Hi there";
$email[] = "Thanks for registering, hope you like it";

mail("someone@example.com", ...$email);

You can put Put all the parameters in an array, and PHP will handle it all for you :)

The above is the detailed content of What are php variable parameters?. 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