Asked  7 Months ago    Answers:  5   Viewed   38 times

How do I write a function that can accept unlimited number of parameters?

What am trying to do is create a function within a class that wraps the following:

$stmt->bind_param('sssd', $code, $language, $official, $percent);



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.

Wednesday, March 31, 2021
answered 7 Months ago

You cannot overload PHP functions. Function signatures are based only on their names and do not include argument lists, so you cannot have two functions with the same name. Class method overloading is different in PHP than in many other languages. PHP uses the same word but it describes a different pattern.

You can, however, declare a variadic function that takes in a variable number of arguments. You would use func_num_args() and func_get_arg() to get the arguments passed, and use them normally.

For example:

function myFunc() {
    for ($i = 0; $i < func_num_args(); $i++) {
        printf("Argument %d: %sn", $i, func_get_arg($i));

Argument 0: a
Argument 1: 2
Argument 2: 3.5
myFunc('a', 2, 3.5);
Wednesday, March 31, 2021
answered 7 Months ago

You can use either:

example($argument1, '', 'test');


example($argument1, NULL, 'test');

You can always check for NULL using instead of empty string:

if ($argument === NULL) {

I think it all depends on what happens inside the function with the args.

Wednesday, March 31, 2021
answered 7 Months ago

You can try something like this:

Route::get('{pageLink}/{otherLinks?}', 'SiteController@getPage')->where('otherLinks', '(.*)');

Remember to put the above on the very end (bottom) of routes.php file as it is like a 'catch all' route, so you have to have all the 'more specific' routes defined first.

class SiteController extends BaseController {

    public function getPage($pageLink, $otherLinks = null)
            $otherLinks = explode('/', $otherLinks);
            //do stuff 


This approach should let you use unlimited amount of params, so this is what you seem to need.

Wednesday, March 31, 2021
answered 7 Months ago

Yes you can do it in C using what are referred to as Variadic Functions. The standard printf() and scanf() functions do this, for example.

Put the ellipsis (three dots) as the last parameter where you want the 'variable number of parameters to be.

To access the parameters include the <stdarg.h> header:

#include <stdarg.h>

And then you have a special type va_list which gives you the list of arguments passed, and you can use the va_start, va_arg and va_end macros to iterate through the list of arguments.

For example:

#include <stdarg.h>

int myfunc(int count, ...)
   va_list list;
   int j = 0;

   va_start(list, count); 
   for(j=0; j<count; j++)
     printf("%d", va_arg(list, int));


   return count;

Example call:

myfunc(4, -9, 12, 43, 217);

A full example can be found at Wikipedia.

The count parameter in the example tells the called function how many arguments are passed. The printf() and scanf() find that out via the format string, but a simple count argument can do it too. Sometimes, code uses a sentinel value, such as a negative integer or a null pointer (see execl() for example).

Friday, June 25, 2021
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :