Home>Article>Backend Development> Detailed explanation of php reference definition and reference passing parameter instance usage
Passing by reference
You can pass a variable to a function by reference so that the function can modify the value of its parameter. The syntax is as follows:
Note that there are no reference symbols in the function call - only in the function definition. The function definition alone is enough for parameters to be passed correctly by reference. In recent versions of PHP, if you use & in foo(&$a); you will get a warning that "Call-time pass-by-reference" is deprecated.
The following can be passed by reference:
Variables, such as foo($a)
New statements, such as foo(new foobar())
References returned from functions , for example:
For detailed explanation, see reference return.
Any other expression cannot be passed by reference, and the result is undefined. For example, the following example of passing by reference is invalid:
These conditions are available in PHP 4.0.4 and later versions.
Reference return
Reference return is used when you want to use a function to find which variable the reference should be bound to. Don't use return references to increase performance, the engine is smart enough to optimize it itself. Only return references if there is a valid technical reason! To return a reference, use this syntax:
value; } } $obj = new foo; $myValue = &$obj->getValue(); // $myValue is a reference to $obj->value, which is 42. $obj->value = 2; echo $myValue; // prints the new value of $obj->value, i.e. 2. ?>
In this example, the properties of the object returned by the getValue function will be assigned values, not copied, just as if no reference syntax was used.
Note: Unlike parameter passing, the ampersand must be used in both places here - indicating that a reference is returned, not the usual copy, and also indicating that $myValue is bound as a reference , instead of the usual assignment.
Note: If you try to return a reference from a function like this: return ($this->value);, this will not work because you are trying to return the result of an expression rather than a referenced variable. You can only return reference variables from functions - there is no other way. If code attempts to return the result of a dynamic expression or the new operator, an E_NOTICE error is issued starting with PHP 4.4.0 and PHP 5.1.0.
$a=test() method calls a function, which just assigns the value of the function to $a. Any changes made to $a will not affect $b in the function, and through $ When calling a function using a=&test(), its function is to point the memory address of the $b variable in return $b and the memory address of the $a variable to the same place, which produces the equivalent of this effect ($a =&b;) So changing the value of $a also changes the value of $b, so after executing $a=&test(); $a=5;, the value of $b becomes 5.
PHP passing parameters by reference usage
Output:
This is a string, and something extra.
If there is no ampersand
Output:
This is a string,
The above is the detailed content of Detailed explanation of php reference definition and reference passing parameter instance usage. For more information, please follow other related articles on the PHP Chinese website!