Viewed   84 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);

 Answers

5

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.

Monday, November 28, 2022
5

You can use either:

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

Or

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.

Tuesday, December 27, 2022
1

You can try something like this:

//routes.php
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.

//controller 
class SiteController extends BaseController {

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

}

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

Wednesday, October 19, 2022
 
1

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', '.'));
?>
Thursday, November 3, 2022
 
marsant
 
1

Use debug_backtrace instead. It will give you the whole trace and doesn't trim arguments as far as I know.

On a second thought: You might get away with it by using

try {
   ...
} catch (Exception $e)
   var_dump($e->getTrace());
}

instead.

Tuesday, November 8, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :