What is the & prefix in PHP? [duplicate] - php

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.

Related

Strange output after calling unset() [duplicate]

This question already has answers here:
unset variable in php
(5 answers)
Closed 4 years ago.
The documentation for NULL says that if I called unset() on a variable, the variable will become NULL:
A variable is considered to be null if:
it has been assigned the constant NULL.
it has not been set to any value yet.
it has been unset().
However, this tutorial says the following will happen when calling unset() on a variable:
PHP looks in the symbol table to find the zval corresponding to this
variable, decrements the refcount, and removes the variable from the
symbol table. Because the refcount is now zero, the garbage collector
knows that there is no way of accessing this zval, and can free the
memory it occupies.
Now I tried the following code:
<?php
$x = 12345;
unset($x);
echo gettype($x);
?>
The output I got is strange, I got an error that says that the variable is undefined (which conforms with the second quote I have posted), but I also got the type of the variable which is NULL (which conforms with the first quote I have posted):
Why am I getting this strange output?
unset() destroys the specified variables.
It does not make the Variable NULL so the warning absolutely makes sense
Notice: Undefined variable: x
Reference : http://php.net/manual/en/function.unset.php
unset() destroys the specified variables. Note that in PHP 3, unset() will always return TRUE (actually, the integer value 1). In PHP 4, however, unset() is no longer a true function: it is now a statement. As such no value is returned, and attempting to take the value of unset() results in a parse error

How is it possible to reference to a variable outside of a code block when it is declared inside a block? [duplicate]

This question already has answers here:
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 8 years ago.
I am just a little confused about this situation.
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$un = $row['USERNAME'];
$pw = $row['PASSWRD'];
$at = $row['ACCOUNT_TYPE'];
$GLOBALS['fn'] = $row['FNAME'];
}
So this code is inside a function. The variables $un, $pw and $at are all declared and given a value inside this block of code.
Now my understanding is that variables that are declared in a block of code are only able to be used in that block.
As you can see I also have a $GLOBALS['fn'] variable setup which I use in an other file so that makes sense to me to make it global.
Now my question is:
How is it possible to reference to a variable outside of a code block when it is declared inside a block?
According to this article here PHP Variable scope the variables declared outside of a function are not the same as inside the function even if they share the same name. For this I would need to prepend the variables inside the block with global or an $GLOBALS array.
NOTE to the duplicate - this may have been asked before I do not really doubt that since it is so simple. I just related it to my situation in my own words. Helps me understand it better.
Also the question where this links to explains what exactly what I read in the linked article. It does however not address the while loop issue I was referring to. So in that case it is a bit different I believe.
Now my understanding is that variables that are declared in a block of code are only able to be used in that block
Wrong, if you mean {} as a block.
According to PHP Manual
Any variable used inside a function is by default limited to the local function scope.
There is no mention of {} level scope within a function. Any variable declared inside a function is available throughout it, even if it as declared inside any sub braces. That's why its still available. Your variables like $un can be accessed even outside the loop, just that they will contain the values from last iteration.
maybe this answer might shed some light.
If I understand correctly, you answered your own question. By declaring variables on the $GLOBALS array, from within a function, you make that variable accessible throughout your scripts.
From the docs:
The $GLOBALS array is an associative array with the name of the global variable being the key and the contents of that variable being the value of the array element. Notice how $GLOBALS exists in any scope, this is because $GLOBALS is a superglobal

'&' sign before function name in PHP [duplicate]

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.

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.

php using '&' operator [duplicate]

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.

Categories