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.
Related
This question already has an answer here:
Variable variable Superglobals
(1 answer)
Closed 8 years ago.
So, I have this code:
$str = '_POST';
print_r($$str);
//Works just fine
function request_var($name){
global $_POST;
$str = '_POST';
print_r($$str);
}
request_var('username');
// _POST not defined
And what the request_var function was supposed to do was loop through the $_POST, $_GET, and $_COOKIE variables (in that order) and if it finds the passed variable name, stop looking and return that value.
I had a system awhile back that worked as intended, but for some reason now it tells me _POST not defined and I can't figure out why? The first part works without errors, but the second part doesn't work at all. Isn't $_POST a super global? (Note: I only added in the global $_POST to see if that would fix it)
Any ideas?
EDIT: Based on the answer provided by another user ( can't see their name in the edit screen), my work around is thus:
function request_var($name,$default='',$force_type=false){
$end = $default;
$arrays = [$_POST,$_GET,$_COOKIE];
foreach($arrays as $array){
if(isset($array[$name])){
$end = $array[$name];
break;
}
}
return ($force_type) ? settype($end,gettype($default)) : $end;
}
echo request_var('username');
//Works just fine
Warning Please note that variable variables cannot be used with PHP's Superglobal arrays within functions or class methods.
source
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:
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.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Reference - What does this symbol mean in PHP?
The third line of the following code, works well in my local environment but returns an error on my production server.
What does the & prefix mean and why is it returning an error on my production server?
Below is my code;
function add_real_escape_string($value) {
if (is_array($value)) {
foreach($value as &$item) {
$item = add_real_escape_string($item);
}
} else {
if (get_magic_quotes_gpc()) {
$value = stripslashes($value);
}
$value = mysql_real_escape_string($value);
}
return $value;
}
The error returned is:
Parse error: syntax error, unexpected '&', expecting T_VARIABLE or '$' in init.php on line 345
It basically is an assign by references.
More about this from the php manual can be found here!
The & means that it passes the reference rather than a copy, meaning that all changes you make to that object will also reflect outside of the scope it is used in. See http://php.net/manual/en/language.references.pass.php for more info.
To explain the error, we'd have to know what error you're getting. Would you mind pasting it for us?
the & prefix is a reference operator. It's a princple inherited from lower-level languages such as C. It means that rather than giving a copy of the variable to a function, or a loop operator etc, you ask PHP to pass its adress in memory. That way the variables given by reference are only declared once in the memory. More, you can do every typical variables operations on it...
You could also check Pointers, the ancestors of references inherited from C.
When you prefix a variable with an &, that means you are grabbing the reference the that variable.
For example, you can pass by reference.
Makes the argument be passed as reference: http://www.php.net/manual/en/functions.arguments.php
It's for passing a variable by reference, meaning if you change it within the function it will change the variable that was passed from outside as well.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Reference - What does this symbol mean in PHP?
hi i have trouble to understand some of the & operator usage. i have come across with multiple examples and point out only those whitch i dont know what they really do ...
what does it mean when i'm:
1) using & in function name
function &foo() {}
2) using & in function parameter
function foo($id, &$desc) {}
3) usgin & in loop
foreach ($data as $key => &$item) {}
function &foo() {}
Returns a variable by reference from calling foo().
function foo($id, &$desc) {}
Takes a value as the first parameter $id and a reference as the second parameter $desc. If $desc is modified within the function, the variable as passed by the calling code also gets modified.
The first two questions are answered by me in greater detail with clearer examples here.
And this:
foreach ($data as $key => &$item) {}
Uses foreach to modify an array's values by reference. The reference points to the values within the array, so when you change them, you change the array as well. If you don't need to know about the key within your loop, you can also leave out $key =>.
The PHP manual has a huge section on references (the & operator) explaining what they are and how to use them.
In your particular examples:
1) Is a return by reference. You need to use the & when calling the function and when declaring it, like you have above: Return by Reference
2) Is passing a parameter by reference. You only need to use the & in the function definition, not when calling the function: Passing by Reference
3) Is using a reference in a foreach loop. This allows you to modify the $item value within the originating array when you're inside the loop.
All the complete information on PHP references is available in the manual.
The PHP documentation on references will answer all of those questions.