Unlimited arguments for PHP function? [duplicate] - php

This question already has answers here:
PHP function with unlimited number of parameters
(8 answers)
Closed 2 years ago.
In php how would you create a function that could take an unlimited number of parameters: myFunc($p1, $p2, $p3, $p4, $p5...);
My next question is: how would you pass them into another function something like
function myFunc($params){
anotherFunc($params);
}
but the anotherFunc would receive them as if it was called using anotherFunc($p1, $p2, $p3, $p4, $p5...)

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.

Previously you should have used func_get_args(), but in a new php 5.6 (currently in beta), you can use ... operator instead of using .
So for example you can write :
function myFunc(...$el){
var_dump($el);
}
and $el will have all the elements you will pass.

Is there a reason why you couldn't use 1 function argument and pass all the info through as an array?

Check out the documentation for variable-length argument lists for PHP.
The second part of your question refers to variable functions:
...if a variable name has parentheses appended to it, PHP will look for a function with the
same name as whatever the variable evaluates to, and will attempt to execute it.

Related

why exactly should I put the ellipsis inside the parameter of a function? [duplicate]

This question already has answers here:
PHP | What are three dots before a function arguments?
(1 answer)
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 3 years ago.
What is the real purpose of function with ellipsis in its parameter?
I have this function:
class Dog{
public function type(...$numbers){
var_dump($numbers);
}
}
and this function
class Dog{
public function type($numbers){
var_dump($numbers);
}
}
Whether I put ellipsis or not, if I call the type function putting multiple parameters in it, its type will always be an array.
So my question is, why exactly should I put the ellipsis inside the parameter of a function?
It's just syntactic sugar, called variable-length argument lists. It lets you pass the function multiple arguments that it will turn into an array automatically. In that example, it would let you call type(1, 2, 3) and $numbers would be an array of those three numbers.

Modify php default function parameter [duplicate]

This question already has answers here:
Only Variables can be passed by reference error
(2 answers)
Closed 5 years ago.
I have a function yaz_wait() which looks like this
mixed yaz_wait ([ array &$options ] ) and as parameters, it has options as you can see in the linked documentation.
One of the options is timeout value which I want to use and edit from its default 15 seconds to some other value.
I have tried
yaz_wait(array("timeout" => 30));
but I get Fatal error: Only variables can be passed by reference...
I am not sure how exactly should I insert this parameter into this function since I have never met with such parameter type (haven't been working with php a lot).
When you have a function with & parameter in a function this means it will return a reference to the variable instead of the value.
In other words, you need to pass a variable that the function will attempt to change(or do whatever with it). Since you aren't passing a variable, you get a fatal error.
Try changing your code to:
$some_arr = array("timeout" => 30);
yaz_wait($some_arr);

Passing an Array as Arguments in php [duplicate]

This question already has answers here:
How can I bind an array of strings with a mysqli prepared statement?
(7 answers)
Closed 1 year ago.
I'm trying to prepare a sql statement with unknown amount of parameters! These parameters are past on in an array. The normal syntax for the function would be:
$stmt->bind_param("string of types",param1,param2,...,paramN)
The problem is I dont know how to add parameters in the function $stmt->bind_param out of an array
I have this code but it does not work:
$stmt= $conn->prepare($request['query']);
if(isset($request['params'])){
call_user_func_array('$stmt->bind_param',$request['params']);
}
$stmt->execute();
$result = $stmt->get_result();
$request['params'] contains the right parameters that need to be added in the function.
But the call_user_func_array gives me this error:
call_user_func_array() expects parameter 1 to be a valid callback, function '$stmt->bind_param' not found or invalid function.
I think call_user_func_array might not be the right function to use!
I googled for hours but could not find a solution for this small problem.
If you're using PHP 5.6+, you could also use the splat operator rather than using call_user_func_array, e.g.
$stmt->bind_param($types, ...$request['params']);
This would be neater for your use-case, since there's an initial argument to bind_param that would otherwise need to be shift-ed onto the front of the array of arguments.
To use a method as a callback parameter to call_user_func_array, use an array with the object name and the method name in it as the callback parameter.
Check PHP's call_user_func_array documentation page for further explanations: http://php.net/manual/pt_BR/function.call-user-func-array.php
// Call the $foo->bar() method with 2 arguments
$foo = new foo;
call_user_func_array(array($foo, "bar"), array("three", "four"));
Try something like:
call_user_func_array(array($stmt,'bind_param'),$request['params']);

What is the difference in the code [duplicate]

This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 9 years ago.
The below code is for sanitizing the posted values. Can some tell me What is the difference between,
<?php
function sanitize_data(&$value, $key) {
$value = strip_tags($value);
}
array_walk($_POST['keyword'],"sanitize_data");
?>
and
<?php
function sanitize_data($value, $key) {
$value = strip_tags($value);
}
array_walk($_POST['keyword'],"sanitize_data");
?>
Thanks
The first uses value as a refrence, so any time you call it with some variable the variable will be changed in the outer scope, not only in the function itself.
Look in the manual for 'reference' if you want more info.
It's called 'pass by reference'. &$value will relate to the original $value passed into the function by pointer, rather than working on a function version.
Please see the PHP Manual.
The first function the value of the first parameter is passed by reference and in the second not. If the variable is passed by referenced, changes to it will also be done on the value outside of the function scope (in the scope you call the function).
Also read the PHP documentation (pass by reference) and is also demonstrated on the array_walk doc page.
First method is called as "Passing value as reference".
So $_POST array values are changed .
In second method will not change the value of $_POST
You can check SO Link: Great Explanation about it.
https://stackoverflow.com/a/2157816/270037
The first function gets $value passed by reference so it can modify it directly, the second function gets passed $value's value.

Do I have to always pass all arguments to PHP function? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP - Calling functions with multiple variables
function test($var1=null, $var2=null, $var3=null){
//smart stuff goes here
}
Do I have to every time call the function passing all variables?
test(null, $var2, null);
I'd like to pass only $var2 because all the other variables have default values... Is it even possible?
In JavaScript we can pass an object to the function, is there something similar in PHP?
You only have to pass the arguments up to and including the last argument you do not wish to use the default value for. In your example, you could do this:
test(null, $var2);
The last argument can be omitted since the default value is satisfactory. But the first one must be included so PHP knows that you are setting the value for the second parameter.
Until PHP offers named parameters like Python does, this is how it has to work.

Categories