I am using the ...$args to populate my function with arguments, but i came across a weird behavior.
I have two test functions:
function withToken(...$arguments)
{
$params = [];
$params['first'] = 'first';
if (count($arguments) > 0) {
foreach ($arguments as $key => $value) {
$params[$key] = $value;
}
}
return $params;
}
and
function normal($arguments)
{
$params = [];
$params['first'] = 'first';
if (count($arguments) > 0) {
foreach ($arguments as $key => $value) {
$params[$key] = $value;
}
}
return $params;
}
In my head they do the same thing, so why calling
withToken([
'second' => 'second',
'third' => 'third'
]);
returns
Array
(
[first] => first
[0] => Array
(
[second] => second
[third] => third
)
)
and calling
normal([
'second' => 'second',
'third' => 'third'
]);
returns
Array
(
[first] => first
[second] => second
[third] => third
)
On PHP documentation i didn't found anything about it, someone could explain me why such behavior is happening?