PHP function to calculate number of parameters passed to a function - php

Is there a PhP function that can count the number of parameters passed to another function?
for example
function tobeCounted ("numberOne","numberTwo","numberThree")
{
//do something with the parameters
}
a function that will count the number of parameters passed to the function above then return three or the indexes of the parameters or parameters themseleves.

If you use func_num_args() then you get the amount of arguments, rather than func_get_args that gets you the array of arguments.

func_get_args

You need these:
func_num_args — Returns the number of arguments passed to the function
func_get_args — Returns an array comprising a function's argument list
From here:
http://www.php.net/manual/en/function.func-num-args.php

Or more directly: func_num_args

func_num_args()
Use this to get the number of parameters passed

Related

How to dynamically call PHP method and pass multiple variables not in an array?

I want to be able to call a php function, but only if the number of elements input, matches the number of elements accepted by the function and if it does match i want to pass them 1 by 1 and not as an array.
Currently i have the following code which does the job, but is messy.
call_user_func($functionname, $vars);
This was hitting problems when i had a function that expected an array of 4 elements, but was only passed 3. Having to check and make sure all the vars exist makes writing functions a bit messy. I have a better solution based on reflection. how to dynamically check number of arguments of a function in php , but im one final step short. If the number of arguments matches the number of arguments in a function, i can call that method, but i dont know how to call that method correctly. I have an array of variables. I need to pass these into the function so that I can do something like the following.
function foo($var1, $var2, $var3)
{
//I know all 3 vars exist and dont need to verify first
}
if(count($arrayof3elements) == get_method_var_count("foo") {
dynamically_call_method("foo", $arrayof3elements);
}
Instead of using call_user_func, use call_user_func_array - this allows you to pass an array of arguments, but passes them to the method as separate things.
function x($foo, $bar) {
echo $foo . $bar;
}
call_user_func_array('x', ['abc', 'def']); // prints abcdef

array values do not work as expected

I've a soap function which expects 3 parameters that should be passed as strings with quotes.
function('id','username','password');
and in another hand i've an array which contains :
[0] = > "'id','username','password'"
[1] = > "'id','username','password'"
....
when i echo $array[0] out put is 'id','username','password' and when i use function('id','username','password'); there is no problem but when i use
function($array[0]); it won't work.
i tested my array with echo, die, print_r ... the output is the same as the function expects!!!!
any help ?
thanks ; )
Simply because it can't work. If you have a function that needs 3 parameters, you can't pass a single parameter. Also if is an array that contains the 3 parameters you need, the function still want and need 3 parameters. Thus, if you give the function an array, it will use just the array as the first one (so you'll have an unexpected behavior) and take the second and the thirs as NULL.
It's true that php is a little bit magic, but can't do miracles.
You need to change the signature of your function.
function('id','username','password');
is a function with three parameters.
function($array[0]);
is a function with only one parameter.

Is there something like JavaScript's apply function in PHP?

In JavaScript, I can use apply to pass an array as arguments to a function:
var f = function (n,m) {},
args = [1,2];
f.apply(null, args);
I now need to do something similar in PHP i.e. pass an array of items as 'separate' arguments to a function.
Is there any way this can be done?
You can use the function call_user_func_array. Simply pass in your function (as a callback, usually a string with the function name), and an array of arguments.
Additional note: for static functions, use forward_static_call_array.

How to call a function with a variable number of parameters which correspond to an array values?

OK, I know it sounds weird but I need to make a function that will receive two parameters the first one is a string and the second an array (containing strings).
This function will then call sprintf. My first parameter will be $format and my array will correspond to the various $args.
How can I achieve this (if possible)?
Thanks!
Well you want the vsprintf() function.
Like Orbling answered, for this particular case you need vsprintf.
But, in a generic case, to call a function with variable number of parameters, you can use func_get_args() inside the function which you desire to accept multiple (any number of variable arguments). This function (when called inside your function) returns an array containing all the parameters passed while calling your function.

php: get variables visible in function

is there any way to get list of variables that were defined in function, or list of all accessible variables in a function?
There is get_defined_vars():
This function returns a multidimensional array containing a list of all defined variables, be them environment, server or user-defined variables, within the scope that get_defined_vars() is called.
it has to be called within each function you want to analyze, though.
http://php.net/manual/en/function.get-defined-vars.php
Documentation indeed ^^
func_num_args — Returns the number of arguments passed to the function
func_get_arg — Return an item from the argument list
func_get_args — Returns an array comprising a function's argument list

Categories