2

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

nyedidikeke
  • 6,899
  • 7
  • 44
  • 59
A-OK
  • 3,184
  • 10
  • 34
  • 42
  • 1
    The `&` is to declare a reference in php, but what error is it returning? – Konerak Mar 27 '11 at 17:50
  • The error is: Parse error: syntax error, unexpected '&', expecting T_VARIABLE or '$' in init.php on line 345 – A-OK Mar 27 '11 at 17:53

6 Answers6

4

It basically is an assign by references.

More about this from the php manual can be found here!

Bjoern
  • 15,934
  • 4
  • 43
  • 48
3

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?

tvkanters
  • 3,519
  • 4
  • 29
  • 45
  • Parse error: syntax error, unexpected '&', expecting T_VARIABLE or '$' in init.php on line 345 – A-OK Mar 27 '11 at 17:53
  • Try putting the & before $value rather than $item. – tvkanters Mar 27 '11 at 17:54
  • Doesn't help, still reports an error. Works properly on my localhost. Hosting solution uses PHP 4.4.7 so that's probably the problem. I'll get them to update the version if possible. – A-OK Mar 27 '11 at 17:59
  • 1
    Alright. By the way, if your host supports PHP5 but doesn't use it by default (like my host :<) try putting this in the .htaccess file to force PHP5 usage: http://pastebin.com/UWGLnsTw – tvkanters Mar 27 '11 at 18:02
2

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.

Oleiade
  • 6,156
  • 4
  • 30
  • 42
1

When you prefix a variable with an &, that means you are grabbing the reference the that variable.

For example, you can pass by reference.

Mike Lewis
  • 63,433
  • 20
  • 141
  • 111
1

Makes the argument be passed as reference: http://www.php.net/manual/en/functions.arguments.php

Mike Soule
  • 732
  • 1
  • 6
  • 11
1

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.

Cyclone
  • 17,939
  • 45
  • 124
  • 193