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.
<?php $a = 100; //声明变量a $b = &$a; //声明变量b,引用自变量a echo "$a <br />"; echo "$b <br />"; $a++; //变量a自增1 echo "$a <br />"; echo "$b <br />";//查看变量b,也增加了1,说明使用的是同一存储单元 ?>
Program execution result:
100 100 101 101
Many people misunderstand that references in PHP are the same as pointers in C. In fact, they are not, and they are very different. 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:
<?php $a = 20; $b = $a; $a = $a + 10; echo $a.' and '.$b; ?>
Program execution result:
30 and 20
If you want to pass it as an address, you need to add &, that is:
<?php $a = 20; $b = &$a; $a = $a + 10; echo $a.' and '.$b; ?>
Program execution result:
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:
<?php $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. Here’s another example:
<?php function foo(&$val1, $val2) { $val1 += 1; $val2 += 1; } $a=5; $b=10; foo($a,$b); echo $a; echo $b; ?>
To run this code, you pass two parameters to the function, one is a reference to the content of $a, and the other is the value of $b. After executing this function, it is found that the content of $a has changed, while the content of $b is No changes.
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:
<?php 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, the return reference of find_var is assigned instead of 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.