PHP: variable-length argument list by reference?
PHP 5.6 introduced new variadic syntax which supports pass-by-reference. (thanks @outis for the update)
function foo(&...$args) {
$args[0] = 'bar';
}
For PHP 5.5 or lower you can use the following trick:
function foo(&$param0 = null, &$param1 = null, &$param2 = null, &$param3 = null, &$param4 = null, &$param5 = null) {
$argc = func_num_args();
for ($i = 0; $i < $argc; $i++) {
$name = 'param'.$i;
$params[] = & $$name;
}
// do something
}
The downside is that number of arguments is limited by the number of arguments defined (6 in the example snippet).
but with the func_num_args() you could detect if more are needed.
Passing more than 7 parameters to a function is bad practice anyway ;)
Variable-length by-ref argument lists in functions
The question seems horrible, but lets humour you. Below is a horrible hack, but you could send across a single argument which contains the items that you want to work with.
function something_else($args) {
foreach ($args as &$arg) {
$arg *= 2;
}
}
$a = 1;
$b = 3;
something_else(array(&$a, &$b));
echo $a . $b; // 26
PHP - Pass array as variable-length argument list
Use
ReflectionFunction::invokeArgs(array $args)
or
call_user_func_array( callback $callback, array $param_arr)
PHP variable length arguments?
Unlike Python's *
operator or C#'s params
keyword, in PHP you don't even have to specify the variable length arguments. As the second part starts off, "No special syntax is required."
As to the rest of the second paragraph: if you want to specify any required or unrelated arguments that come before the variable-length arguments, specify them in your function signature so your function can handle those. Then to get the variable-length arguments, remove the required variables from func_get_args()
, like so:
function func($required) {
// Contains all arguments that come after $required
// as they were present at call time
$args = array_slice(func_get_args(), 1);
}
You don't have to do this (you can still slice from func_get_args()
and use its different elements accordingly), but it does make your code more self-documenting.
What should PHP's variable-length argument `...` token be referred to as?
TL;DR: The tokens name is 'T_ELLIPSIS', used in a function declaration makes this function 'variadic', used when calling a function with an array holding the parameters, it's called 'argument unpacking'.
Its name is T_ELLIPSIS
, I found that out using token_get_all
and token_name
in a psysh-session:
>>> token_get_all('<?php function testit(...$a) { echo $args;}')
=> [
[
379,
"<?php ",
1,
],
....
[
391,
"...",
1,
],
....
>>> token_name(391)
=> "T_ELLIPSIS"
>>>
Edit: I understood you possibly too literally -- I thought you asked for the token-name, but given the downvote(s), I suspect you meant, how do programmers refer to it in speech and writing (apparently "splat", according to the comments)
Edit 2: Used in a function definition before the last parameter, in other languages a function defined with an ellipsis in PHP, would be called a 'variadic function':
>>> function f(...$a) { return $a; }
>>> f(1, 2, 3, 4)
=> [
1,
2,
3,
4,
]
>>>
Edit 3: Finally: If you have an array holding values you want to pass to a function, you can use ...
to achieve "Argument Unpacking"
>>> function f($a, $b, $c) { return "{$a}-{$b}-{$c}"; }
>>> f(...[1,2,3]);
=> "1-2-3"
>>>
Create a variable parameter list
To use variable length parameters on PHP you need the function func_get_args()
instead to define the parameters on function declaration. Your function look like this:
function foo()
{
$params_count = func_num_args();
$params_values = func_get_args();
}
On $params_values
there are all parameters which were given to the foo()
function. On params_count
there is the number of parameters given to foo()
. You can get the number of parameters given to the foo function with func_num_args()
An example of using this functions (https://3v4l.org/TWd3v):
function foo() {
$params_count = func_num_args();
var_dump($params_count);
$params_values = func_get_args();
var_dump($params_values);
}
Function with variable length argument list
No, you cannot. Use arrays:
function foo($args) {
extract($args);
echo $bar + $baz;
}
foo(array("bar" => 123, "baz" => 456));
Write a bug report on php.net and ask them to add named arguments to the language!
How to pass variable number of arguments to a PHP function
If you have your arguments in an array, you might be interested by the call_user_func_array
function.
If the number of arguments you want to pass depends on the length of an array, it probably means you can pack them into an array themselves -- and use that one for the second parameter of call_user_func_array
.
Elements of that array you pass will then be received by your function as distinct parameters.
For instance, if you have this function :
function test() {
var_dump(func_num_args());
var_dump(func_get_args());
}
You can pack your parameters into an array, like this :
$params = array(
10,
'glop',
'test',
);
And, then, call the function :
call_user_func_array('test', $params);
This code will the output :
int 3
array
0 => int 10
1 => string 'glop' (length=4)
2 => string 'test' (length=4)
ie, 3 parameters ; exactly like iof the function was called this way :
test(10, 'glop', 'test');
Related Topics
Phpmailer Not Sending and Not Giving Error
How to Display PHP Code in HTML
How to Pass Variables Between PHP Scripts
PHP Upload Size and Its Impact on Post Size and Memory Limit
PHP - How to Get Shell Errors Echoed Out to Screen
PHP Curl: How to Set Body to Binary Data
Laravel Input Facade VS Request Facade
How to Use MySQL Found_Rows() in PHP
Why Composer Can Not Find My Brand New Package on Packagist.Org
How to Disable or Encrypt "View Source" for My Site
Is There a Call_User_Func() Equivalent to Create a New Class Instance
Converting to Date in PHP from Yyyymmdd Format