PHP function with unlimited number of parameters
The above suggests are all good, but I don't think they will be suitable for your situation.
$stmt->bind_param('sssd', $code, $language, $official, $percent);
If you want to wrap this function, you will need to pass references to the original argument variables to the bind_param function. I don't think func_get_args() gives you this, it gives you values instead. Thus it won't be possible to use these to pass to the parent function. I battled with a similar issue when trying to extend mysqli_stmt and never came to satisfactory solution.
This is not really an answer to your question I'm afraid, just a warning that other arguments may not work in your particular application of arbitrary number of arguments.
Can a PHP function accept an unlimited number of parameters?
In PHP, use the function func_get_args
to get all passed arguments.
<?php
function myfunc(){
$args = func_get_args();
foreach ($args as $arg)
echo $arg."/n";
}
myfunc('hello', 'world', '.');
?>
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.
<?php
function myfunc($args){
while(list($var, $value)=each($args))
echo $var.' '.$value."/n";
}
myfunc(array('first'=>'hello', 'second'=>'world', '.'));
?>
Unlimited arguments for PHP function?
call_user_func_array('anotherFunc', func_get_args());
func_get_args
returns an array containing all arguments passed to the function it was called from, and call_user_func_array
calls a given function, passing it an array of arguments.
Functions With Unlimited Arguments in php
Use func_get_args()
to access all arguments (as an array) passed to the function. Additionally, you can use func_num_args()
to get a count of all arguments passed in.
function make_my_merge () {
if ( func_num_args() ) {
$args = func_get_args();
echo join( ", ", $args );
}
}
// Foo, Bar
make_my_merge("Foo", "Bar");
// Foo, Bar, Fizz, Buzz
make_my_merge("Foo", "Bar", "Fizz", "Buzz");
Codepad: http://codepad.org/Dk7MD18I
PHP Functions - Maximum number of arguments
Arguments to a function are pushed on a stack, after which the function is called which in turn reads the stack and uses those values as parameters.
So as long as the stack isn't full, you can keep adding parameters, but it'll depend on the situation, and at design-time you won't know the stack size.
But I really hope this is pure a technical discussion and you don't need it IRL. ;-)
How to work with infinity arguments in a function (like PHP's isset())
func_get_args
will do what you want:
function infinite_parameters() {
foreach (func_get_args() as $param) {
echo "Param is $param" . PHP_EOL;
}
}
You can also use func_get_arg
to get a specific parameter (it's zero-indexed):
function infinite_parameters() {
echo func_get_arg(2);
}
But be careful to check that you have that parameter:
function infinite_parameters() {
if (func_num_args() < 3) {
throw new BadFunctionCallException("Not enough parameters!");
}
}
You can even mix together func_*_arg
and regular parameters:
function foo($param1, $param2) {
echo $param1; // Works as normal
echo func_get_arg(0); // Gets $param1
if (func_num_args() >= 3) {
echo func_get_arg(2);
}
}
But before using it, think about whether you really want to have indefinite parameters. Would an array not suffice?
How to pass variable number of arguments to a PHP function
If you have your arguments in an array, you might be interested by the call_user_func_array
function.
If the number of arguments you want to pass depends on the length of an array, it probably means you can pack them into an array themselves -- and use that one for the second parameter of call_user_func_array
.
Elements of that array you pass will then be received by your function as distinct parameters.
For instance, if you have this function :
function test() {
var_dump(func_num_args());
var_dump(func_get_args());
}
You can pack your parameters into an array, like this :
$params = array(
10,
'glop',
'test',
);
And, then, call the function :
call_user_func_array('test', $params);
This code will the output :
int 3
array
0 => int 10
1 => string 'glop' (length=4)
2 => string 'test' (length=4)
ie, 3 parameters ; exactly like iof the function was called this way :
test(10, 'glop', 'test');
Related Topics
Smtp Configuration for PHP Mail
Access Query String Values from Laravel
How to Check If String Is a Valid Xml Element Name
Woocommerce: Assigning an Endpoint to a Custom Template in My Account Pages
Why Does the Laravel API Return a 419 Status Code on Post and Put Methods
Google Gcm Server Returns Unauthorized Error 401
PHP - Split String in Key/Value Pairs
How to Generate Random 64-Bit Value as Decimal String in PHP
Comma Separated Values in MySQL "In" Clause
Smtp Server Response: 530 5.7.0 Must Issue a Starttls Command First
PHP Function with Unlimited Number of Parameters
Disable Deprecated Warning in Symfony 2(.7)
Use a Variable to Define a PHP Function
Run Shell Command and Send Output to File
Can You Put PHP Inside PHP with Echo
More Concise Way to Check to See If an Array Contains Only Numbers (Integers)