Home >Backend Development >PHP Tutorial >Detailed analysis of the usage of PHP quotation character &_PHP tutorial

Detailed analysis of the usage of PHP quotation character &_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 14:59:151255browse

Regarding the role of PHP references (that is, adding the ampersand in front of variables, functions, objects, etc.), let's first look at the following program.

Copy code The code is as follows:

$a = 100; //Declare variable a
$b = &$a; //Declare variable b, reference independent variable a
echo "$a
";
echo "$b
";
$a++; //Variable a increases by 1
echo "$a
";
echo "$b
";//View variable b, also increases 1, indicating that the same storage unit is used
?>

Program running result:
Copy code Code As follows:

100
100
101
101

Many people misunderstand that references in php are the same as pointers in C. In fact, they are not. So, and it makes a big difference. Except for the pointers in C language that do not need to be explicitly declared during the array transfer process, other points need to be defined using *. However, the pointer to address (similar to a pointer) function in PHP is not implemented by the user himself, but is implemented by the Zend core. Yes, the reference in PHP adopts the principle of "copy-on-write", that is, unless a write operation occurs, variables or objects pointing to the same address will not be copied.

php defaults to passing by value:
Copy code The code is as follows:

$a = 20;
$b = $a;
$a = $a + 10;
echo $a.' and '.$b;
?>

Program running results:
Copy code The code is as follows:

30 and 20

If you want to change it to address transmission, you need to add &, that is:
Copy the code The code is as follows:

< ?php
$a = 20;
$b = &$a;
$a = $a + 10;
echo $a.' and '.$b;
?> ;

Program running result:
Copy code The code is as follows:

30 and 30

In other words, & passes the address of $a to $b. In this case, the two variables now share a memory storage area, which means that their values ​​are the same.

The same syntax can be used in functions, which return references, and in the new operator:
Copy code The code is as follows:

$bar =& new fooclass();
$foo =& find_var($bar);
?>

The second thing a reference does is pass a variable by reference. This is accomplished by creating a local variable within the function that references the same content in the calling scope. To put it simply, the parameter of a function is a reference to a local variable. Let’s give another example:
Copy code The code is as follows:

function foo(&$ val1, $val2) {
$val1 += 1;
$val2 += 1;
}
$a=5;
$b=10;
foo($ a,$b);
echo $a;
echo $b;
?>

Running this code passes two parameters to the function, one is a reference The content of $a is the value of $b. After executing this function, it is found that the content of $a has changed, but the content of $b has not changed.

The third usage of PHP references is reference return. This usage is a bit difficult to understand. Reference return is used when you want to use a function to find which variable the reference should be bound to. When returning a reference, use this syntax: To put it simply, it is still the return of the reference function. But unlike parameter passing, the & symbol must be used in both function definition and function reference. Here's an example:
Copy code The code is as follows:

function &find_var ($param)
{
/* ...code... */
return $found_var;
}
$foo =& find_var ($bar);
$foo->x = 2;
?>

In this example, the assignment to $foo is the return reference of the function find_var , so when assigning a value to $foo->x, you assign a value to the return reference of find_var, rather than a simple assignment.

The last usage of PHP references is reference positioning. There are two main applications: one is global reference. When declaring a variable with global $var, a reference to the global variable is actually established. That is, it is the same as $var =& $GLOBALS["var"];. Another one is the usage of $this. In a method of an object, $this is always a reference to the object that calls it.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/328163.htmlTechArticleReferences about php (that is, add lt;?php $a = 100 in front of variables, functions, objects, etc. ; //Declare variable a $b = //Declare variable b, refer to independent variable a echo "$a br /"; echo "$b br /"; $a++;...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn