Tahazzot
Tahazzot

Reputation: 1262

Give array as a function arguments and get it as normal variable in PHP

At first, see my code:

//normal function
function test($name, $password)
{
    //do stuff...
}

//calling...
test(['anonymous', '2020']);

So, Basically I need a system like this one above. I know that this code is wrong.


What do I need?
I will give an array as a function argument and In my function, I need to get this array as a normal variable.


but is it possible to make these features in another alternative way?

Upvotes: 1

Views: 35

Answers (1)

u_mulder
u_mulder

Reputation: 54841

Splat operator ...:

//normal function
function test($name, $password)
{
    echo $name . ' ' . $password;
}

//calling...
test(...['anonymous', '2020']);

Or call_user_func_array:

//normal function
function test($name, $password)
{
    echo $name . ' ' . $password;
}

//calling...
call_user_func_array('test', ['anonymous', '2020']);

Upvotes: 2

Related Questions