I know how to check with func_num_args for the number of arguments for a function, but how can I get these arguments inside the php function:
<?php
function test() {
echo func_num_args(); //returns 2
echo $argv[1]; //this doesn't work
}
test("aa","bb");
?>
You can call func_get_args() to obtain the array of arguments passed to the function.
For your example, simply add
$args = func_get_args();
And it should work as intended.
There also is func_get_arg, which returns a single argument:
echo func_get_arg(1); // prints second argument
For PHP 5.6+ you can use: ... like this:
function xy(...$args) {
foreach($args as $arg)
echo $arg . "<br />";
}
For more information about this see the manual: http://php.net/manual/en/functions.arguments.php#functions.variable-arg-list
Related
Apologies for the newbie question but i have a function that takes two parameters one is an array one is a variable function createList($array, $var) {}. I have another function which calls createList with only one parameter, the $var, doSomething($var); it does not contain a local copy of the array. How can I just pass in one parameter to a function which expects two in PHP?
attempt at solution :
function createList (array $args = array()) {
//how do i define the array without iterating through it?
$args += $array;
$args += $var;
}
If you can get your hands on PHP 5.6+, there's a new syntax for variable arguments: the ellipsis keyword.
It simply converts all the arguments to an array.
function sum(...$numbers) {
$acc = 0;
foreach ($numbers as $n) {
$acc += $n;
}
return $acc;
}
echo sum(1, 2, 3, 4);
Doc: ... in PHP 5.6+
You have a couple of options here.
First is to use optional parameters.
function myFunction($needThis, $needThisToo, $optional=null) {
/** do something cool **/
}
The other way is just to avoid naming any parameters (this method is not preferred because editors can't hint at anything and there is no documentation in the method signature).
function myFunction() {
$args = func_get_args();
/** now you can access these as $args[0], $args[1] **/
}
You can specify no parameters in your function declaration, then use PHP's func_get_arg or func_get_args to get the arguments.
function createList() {
$arg1 = func_get_arg(0);
//Do some type checking to see which argument it is.
//check if there is another argument with func_num_args.
//Do something with the second arg.
}
i have function like this.
function load($name, $arg1, $arg2, $arg3, $arg4){
$this->$name = new $name($arg1, $arg2, $arg3, $arg4);
}
the load() method will load some class and set it as class property and the infinite arguments, depend in the class they assigned.
another example if i only set method $this->load with 3 argument, the this what will happen in the process
function load($name, $arg1, $arg2){
$this->$name = new $name($arg1, $arg2);
}
it is possible do something like that?
You can use combination of func_get_args(), ReflectionClass and the help of this comment like this:
function load(){
$args = func_get_args();
if( !count( $args)){
throw new Something();
}
$name = array_shift( $args);
$class = new ReflectionClass($name);
$this->$name = $class->newInstanceArgs($args);
}
An easier way of handling this is to use an array for the $arg variables. The other constructors dynamically called (new $name()) would then need to accept an array as their input as well, and you can simply pass through the array of parameters:
// Params in an array
$params = array(1,2,3,4,5);
// Pass into the load() function
function load($name, $params) {
// And pass them through to the dynamic constructor
$this->$name = new $name($params);
}
Unless you set default values on the arguments, you must pass in values when the function is called:
function load($name, $arg1, $arg2, $arg3, $arg4){
load('x'); // fails, didn't specify args 1->4
but with defaults:
function load($name, $arg1, $arg2 = null, $arg3 = null, $arg4 = null){
load('x', 'y'); // works, args 2->4 are optional
load('x'); // fails, didn't specify arg1
load('x', 'y', 'z'); // works, args 3->4 are null.
There are other options - pass in option arguments in an array, or use func_get_arg()
function load($name) {
load('x', 'y', 'z') // not an error, use func_get_args/func_num_args to get the extras
func_get_args() might be what you're looking for. From the PHP Docs:
<?php
function foo()
{
$numargs = func_num_args();
echo "Number of arguments: $numargs<br />\n";
if ($numargs >= 2) {
echo "Second argument is: " . func_get_arg(1) . "<br />\n";
}
$arg_list = func_get_args();
for ($i = 0; $i < $numargs; $i++) {
echo "Argument $i is: " . $arg_list[$i] . "<br />\n";
}
}
foo(1, 2, 3);
?>
The above example will output:
Number of arguments: 3<br />
Second argument is: 2<br />
Argument 0 is: 1<br />
Argument 1 is: 2<br />
Argument 2 is: 3<br />
There is a way to define a function with a truly variable-size arguments in PHP. You need to use func_get_args function in your function:
function load() {
$args = fnc_get_args();
$name = $args[0];
$arg1 = $args[1];
...
}
You'll need to then determine what/how to call next.
Consider the following functions:
function debug() {
$args = func_get_args();
// process $args
}
function debug_die() {
// call debug() with the passed arguments
die;
}
The method debug_die exits after calling debug that takes a variable number of arguments.
So the arguments passed to debug_die as such are meant for debug only and just have to be forwarded. How can this be done in the debug_die method?
function debug_die() {
call_user_func_array("debug", func_get_args());
die;
}
Is there a PHP function to find the number of parameters to be received/passed to a particular function?
func_num_args
Gets the number of arguments passed to the function.
Here is an example taken right from the link above,
<?php
function foo()
{
$numargs = func_num_args();
echo "Number of arguments: $numargs\n";
}
foo(1, 2, 3);
?>
Which outputs,
Number of arguments: 3
Try func_num_args:
http://www.php.net/manual/en/function.func-num-args.php
func_num_args() and func_get_args() to get the value of arguments
From the documentation :
<?php
function foo()
{
$numargs = func_num_args();
echo "Number of arguments: $numargs\n";
}
foo(1, 2, 3);
?>
The above example will output:
Number of arguments: 3
func_number_args() is limited to only the function that is being called. You can't extract information about a function dynamically outside of the function at runtime.
If you're attempting to extract information about a function at runtime, I recommend the Reflection approach:
if(function_exists('foo'))
{
$info = new ReflectionFunction('foo');
$numberOfArgs = $info->getNumberOfParameters(); // this isn't required though
$numberOfRequiredArgs = $info->getNumberOfRequiredParameters(); // required by the function
}
I would like to write a function that (amongst other things) accepts a variable number of arguments and then passes them to sprintf().
For example:
<?php
function some_func($var) {
// ...
$s = sprintf($var, ...arguments that were passed...);
// ...
}
some_func("blah %d blah", $number);
?>
How do I do this in PHP?
function some_func() {
$args = func_get_args();
$s = call_user_func_array('sprintf', $args);
}
// or
function some_func() {
$args = func_get_args();
$var = array_shift($args);
$s = vsprintf($var, $args);
}
The $args temporary variable is necessary, because func_get_args cannot be used in the arguments list of a function in PHP versions prior to 5.3.
use a combination of func_get_args and call_user_func_array
function f($var) { // at least one argument
$args = func_get_args();
$s = call_user_func_array('sprintf', $args);
}
Or better yet (and a bit safer too):
function some_func(string $fmt, ... $args) {
$s = vsprintf($fmt, $args);
}
This is PHP 7.4, not sure if it works in earlier versions.
use $numargs = func_num_args();
and func_get_arg(i) to retrieve the argument
Here is the way:
http://www.php.net/manual/en/functions.arguments.php#functions.variable-arg-list
basically, you declare your function as usual, without parameters, then you call func_num_args() to find out how many arguments they passed you, and then you get each one by calling func_get_arg() or func_get_args(). That's easy :)