开发者

What is the & prefix in PHP? [duplicate]

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 开发者_JAVA百科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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜