A. func_get_argv[] B. func_get_argc[] C. get_argv[] D. get_argc[] Answer: Option B Here is an example- Which one of the following PHP functions can be used to build a function that accepts any number of arguments?
Solution[By Examveda Team]
Join The Discussion
Related Questions on Functions
In PHP there are functions like unset[]
that support any number of parameter we throw at them.
I want to create a similar function that is capable of accepting any number of parameters and process them all.
Any idea, how to do this?
asked Jun 20, 2010 at 6:59
StarxStarx
75.7k45 gold badges181 silver badges259 bronze badges
2
In PHP, use the function func_get_args
to get all passed arguments.
An alternative is to pass an array of variables to your function, so you don't have to work with things like $arg[2];
and instead can use $args['myvar'];
or rewmember what order things are passed in. It is also infinitely expandable which means you can add new variables later without having to change what you've already coded.
answered Jun 20, 2010 at 7:05
Aaron ButacovAaron Butacov
29.4k8 gold badges46 silver badges61 bronze badges
0
You can use these functions from within your function scope:
- func_get_arg[]
- func_get_args[]
- func_num_args[]
Some examples:
foreach [func_get_args[] as $arg]
{
// ...
}
for [$i = 0, $total = func_num_args[]; $i < $total; $i++]
{
$arg = func_get_arg[$i];
}
answered Jun 20, 2010 at 7:17
Alix AxelAlix Axel
148k91 gold badges390 silver badges493 bronze badges
0
You will have 3 functions at your disposal to work with this. Have the function declaration like:
function foo[]
{
/* Code here */
}
Functions you can use are as follows
func_num_args[] Which returns the amount of arguments that have been passed to the array
func_get_arg[$index] Which returns the value of the argument at the specified index
func_get_args[] Which returns an array of arguments provided.
answered Jul 6, 2010 at 23:48
Not AvailableNot Available
2,9157 gold badges25 silver badges31 bronze badges
You can use func_get_args[] inside your function to parse any number of passed parameters.
answered Jun 20, 2010 at 7:02
Peter AnselmoPeter Anselmo
2,9411 gold badge16 silver badges12 bronze badges
If you are using PHP 5.6 or later version, argument lists may include the
...
token to denote that the function accepts a variable number of arguments. The arguments will be passed into the given variable as an array; Simply Using ...
you can access unlimited variable arguments.
for example:
If you are using PHP version