Home > Article > Backend Development > How to express the two suffix parameters in php
In PHP functions, there are two special parameters: variable number of parameters...$var and callback function callable $callback. A variable number of arguments allows a function to receive any number of arguments, stored as an array. Callback functions allow a function to accept a function and execute it under specific conditions.
Two parameters after the PHP function
In the PHP function, there are two special parameters: ...$var
and callable $callback
. They allow functions to receive a variable number of arguments and callback functions.
Variable number of parameters:...$var
function functionName(...$var)
$var
variable in the form of an array. <code class="php">function sum(...$numbers) { $total = 0; foreach ($numbers as $number) { $total += $number; } return $total; } echo sum(1, 2, 3, 4, 5); // 输出: 15</code>
Callback function: callable $callback
function functionName(callable $callback )
<code class="php">function filterArray(array $array, callable $callback) { $filteredArray = []; foreach ($array as $element) { if ($callback($element)) { $filteredArray[] = $element; } } return $filteredArray; } $callback = function ($value) { return $value > 10; }; $filteredArray = filterArray([1, 2, 10, 15, 20], $callback); // 输出: [15, 20]</code>
Note:
The above is the detailed content of How to express the two suffix parameters in php. For more information, please follow other related articles on the PHP Chinese website!