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.
Related
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);
This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 8 years ago.
Can you please explain to me the differences between two functions:
function &a(){
return something;
}
and
function b(){
return something;
}
Thanks!
The first returns a reference to something, the second a copy of something.
In first case, when the caller modify the returned value, something will be modified as a global variable do.
In the second case, modifying a copy as no effect to the source.
An ampersand before a function name means the function will return a reference to a variable instead of the value.
According to this LINK
Returning by reference is useful when you want to use a function to find to which
variable a reference should be bound. Do not use return-by-reference to increase
performance. The engine will automatically optimize this on its own. Only return
references when you have a valid technical reason to do so.
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.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
The ultimate clean/secure function
When it comes to sanitizing POST/GET data could we just program a loop to go through all set variables in a universal php include file and never had to worry about it in code?
I have always done a function called sanitize to do this but this seems to make sense.
You may be better off creating a function in your application that would do it when needed. Then you'll still have the original posted values in case you need them and you can modify the function as needed based on what youre cleansing by passing it options. For example:
function getPostField($field)
{
// all your sanitation and isset/empty checks
$val = sanitize($_REQUEST[$field]);
// ...
return $val;
}
Yes, of course. Some frameworks do this automatically and store the sanitized REQUEST variables in a different array or object, so the original data is still available should it ever be required.
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.