For PHP references (that is, variables or functions, objects, etc., preceded by the & symbol), let's look at the following procedure.
";
";//view variable B, also increased by 1, indicating that the same storage unit is used?>
Program Run Result:
100 100 101 101
Many people misunderstand that the reference in PHP is the same as the pointer in C, which is not, in fact, very different. In addition to the pointer in the C language is not explicitly stated in the process of the array, the others need to be defined using *, and PHP's point-of-address (similar pointer) function is not implemented by the user itself, is implemented by the Zend Core, PHP reference in the use of "copy-on-write" principle, Unless a write operation occurs, a variable or object that points to the same address is not copied.
PHP defaults to pass value:
Program Run Result:
20 and
If you want to change address delivery, you need to add &
Program Run Result:
30 and
That is,,& the address of $ A to $b, so that the two variables now share a memory storage area, meaning their values are the same.
The same syntax can be used in a function, it returns a reference, and is used in the new operator:
The second thing a reference does is pass a variable by reference. This is done by creating a local variable within the function and referencing the same content within the call range. The popular point is that a function parameter is a reference to a local variable. Here is another example:
Running this code is to pass two parameters to the function, one is to refer to $ A content, one is the value of $b, after executing this function, we find that the contents of $ A change, and the contents of $b are unchanged.
The third use of PHP references is a reference return, which is a bit difficult to understand, and reference returns are used when you want to use a function to find out which variable the reference should be bound to. When you return a reference, use this syntax: The simple point, or the return of the reference function. However, unlike parameter passing, the & symbol must be used in both the function definition and the function reference two places. Here's an example:
x = 2;? >
This example assigns a $foo to a function find_var, so assigning a value to $foo->x is a return reference assignment to Find_var instead of a simple assignment.
The last use of PHP references is to reference positioning, there are two main applications: one is the global reference, when declaring a variable with the global $var actually establishes a reference to the global variable. That is, with $var =& $GLOBALS ["var"]; The other is the use of $this, in the method of an object, $this is always a reference to the object that invokes it.
http://www.bkjia.com/PHPjc/752461.html www.bkjia.com true http://www.bkjia.com/PHPjc/752461.html techarticle With reference to PHP (that is, variables or functions, objects, etc.), let's look at the following procedure. php$a = 100;//DECLARE variable a$b =//declare variable B, reference ...