What are php variable parameters?

怪我咯
Release: 2023-03-11 16:54:01
Original
1559 people have browsed it

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");
Copy after login

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);
Copy after login

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!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!