PHP Get All Arguments as Array

PHP get all arguments as array?

func_get_args returns an array with all arguments of the current function.

Print all of the function arguments in PHP

You need to use call_user_func_array instead of your custom function DoPlugin:

function MyPlugin(){
echo implode(" ", func_get_args());
}

$args = array("MyPlugin", "Egg", "Milk");
call_user_func_array(array_shift($args), $args);

The output will be:

Egg Milk

Demo: https://eval.in/99503

PHP get all function arguments as $key = $value array?

PHP does not support an arbitrary number of named parameters. You either decide on a fixed number of parameters and their names in the function declaration or you can only get values.

The usual way around this is to use an array:

function register_template($args) {
// use $args
}

register_template(array('name' => 'my template', ...));

PHP - Get all parameters from a function (even the optional one)

You can accomplish this using the ReflectionFunction function class.

function foo($a, $b=1)
{
$arr = array();
$ref = new ReflectionFunction(__FUNCTION__);
foreach($ref->getParameters() as $parameter)
{
$name = $parameter->getName();
$arr[$name] = ${$name};
}
print_r($arr);

// ...
}

Calling the function:

foo(1);

Output:

Array
(
[a] => 1
[b] => 1
)

Demo

How to pass array as multiple parameters to function?

You need to use call_user_func_array

call_user_func_array( array($method, $function), $params);

How can I convert a PHP function's parameter list to an associative array?

The you can do this using compact:

function myFunc($a, $b, $c) {
$params = compact('a', 'b', 'c');
// ...
}

Or, get_defined_vars() will give you an associative array of all the variables defined in that scope, which would work, but I think this might also include $_POST, $_GET, etc...

Otherwise, you can use func_get_args to get a list of all the arguments passed to the function. This is not associative though, since it is only data which is passed (that is, there's no variable names). This also lets you have any number of arguments in your function.

Or, just specify one argument, which is an array:

function myFunc($params) {

}

compact() seems to be closest to what you're after though.

Can we pass an array as parameter in any function in PHP?

You can pass an array as an argument. It is copied by value (or COW'd, which essentially means the same to you), so you can array_pop() (and similar) all you like on it and won't affect anything outside.

function sendemail($id, $userid){
// ...
}

sendemail(array('a', 'b', 'c'), 10);

You can in fact only accept an array there by placing its type in the function's argument signature...

function sendemail(array $id, $userid){
// ...
}

You can also call the function with its arguments as an array...

call_user_func_array('sendemail', array('argument1', 'argument2'));

Having the variable names in func_get_args()

You can not. Variable names are not passed into functions. Variables are placeholders local to a specific algorithm, they are not data and they do not make sense in another scope and are not passed around. Pass an explicitly named associative array if you need key-value pairs:

bulkParam(['d1' => $d1, 'd2' => $d2, ...]);

A shortcut for this is:

bulkParam(compact('d1', 'd2'));

Then use an array:

function bulkParam(array $params) {
foreach ($params as $key => $value) ...
}

As Mark mentions in the comments, sometimes you don't even have variables in the first place:

bulkParam('foo');
bulkParam(foo(bar(baz())));

Now what?

Or eventually you'll want to refactor your code and change variable names:

// old
$d1 = 'd1';
bulkParam($d1);

// new
$userName = 'd1';
bulkParam($userName);

Your application behaviour should not change just because you rename a variable.



Related Topics



Leave a reply



Submit