With reference to PHP (the role of & symbol in front of a variable or function, object, etc.), let's look at the following procedure.
Copy Code code as follows:
<?php
$a = 100; Declaring variable A
$b = & $a; Declaring variable B, referencing argument a
echo "$a <br/>";
echo "$b <br/>";
$a + +; Variable a self-increase 1
echo "$a <br/>";
echo "$b <br/>";//view variable B, also increased by 1, indicating that the same storage unit is used
?>
Program Run Result:
Copy Code code as follows:
Many people misunderstand that the reference in PHP is the same as the pointer in C, which is actually not the case, and it's a big difference. The pointer in C language is not explicitly declared except in the array pass. All others need to use * to define, and PHP for the address (like a pointer) function is not implemented by the user, is implemented by the Zend Core, PHP referenced by the "write-time copy" principle, Is that unless a write is done, a variable or object that points to the same address is not copied.
PHP defaults to pass values:
Copy Code code as follows:
<?php
$a = 20;
$b = $a;
$a = $a + 10;
echo $a. ' and '. $b;
?>
Program Run Result:
Copy Code code as follows:
If you want to change to address delivery need to add, both:
Copy Code code as follows:
<?php
$a = 20;
$b = & $a;
$a = $a + 10;
echo $a. ' and '. $b;
?>
Program Run Result:
Copy Code code as follows:
That is,,& the $a address to $b, so that the two variables now share an area of memory, meaning their values are the same.
The same syntax can be used in a function that returns a reference and is used in the new operator:
Copy Code code as follows:
<?php
$bar =& new Fooclass ();
$foo =& Find_var ($bar);
?>
The second thing a reference does is to pass a variable with a reference. This is done by creating a local variable within the function, and the variable is referenced within the call scope by the same content. The popular point is that a parameter of a function is a reference to a local variable. Here's another example:
Copy Code code as follows:
<?php
function foo (& $val 1, $val 2) {
$val 1 = 1;
$val 2 = 1;
}
$a = 5;
$b = 10;
Foo ($a, $b);
echo $a;
Echo $b;
?>
Running this code is to pass two parameters to the function, one is to refer to the content of $a, one is the value of $b, after executing this function, found that $a content changed, and $b content is unchanged.
The third use of PHP references is to return the reference, which is a bit difficult to understand, and the reference is used when you want to use a function to find the reference to which variable it should be bound. When you return a reference, use this syntax: To say a simple point, or to refer to the return of a function. However, unlike parameter passing, you must use the & symbol in both function definitions and function references in all two places. Here's an example:
Copy Code code as follows:
<?php
function &find_var ($param)
{
/* ... code ... * *
return $found _var;
}
$foo =& Find_var ($bar);
$foo->x = 2;
?>
This example assigns the $foo to the return reference of the function Find_var, so when assigning a value to the $foo->x, it assigns a value to Find_var's return reference, rather than a simple assignment.
The last use of PHP references is reference positioning, which has two applications: one is a global reference, and when declaring a variable with global $var actually establishes a reference to the global variable. That is, the $var =& $GLOBALS ["var"]; Another is the use of $this, in the method of an object, $this is always a reference to the object that called it.