Function reference uses the & Symbol like variable reference in php. Today we will take a look at some examples returned by function reference. I hope these examples will be helpful to you. function reference uses the & Symbol like variable reference in php. Today we will take a look at some examples returned by function reference. I hope these examples will be helpful to you.
Script ec (2); script
Reference return
This is written in the manual: The reference return is used when you want to use the function to find the variable to which the reference should be bound. Do not use return references to increase performance. The engine is smart enough for optimization. A reference is returned only when there are reasonable technical reasons! To return a reference
PHP allows you to bind the return reference of a function to a variable:
Function & returns_reference ()
{
Static $ someref = 0;
$ Someref ++;
Return $ someref;
}
$ Newref = & returns_reference (); // returns a reference, equivalent to $ newref = & $ someref;
Echo $ newref; // 1
$ Notref = returns_reference (); // The value is a copy.
$ Newref = 100;
Echo $ notref; // 2
$ Newref = 100;
Echo returns_reference (); /// 101
It can be seen that for a function to return a reference, the & operator must be included in the function declaration and assignment.
The same is true for methods in the class:
Class foo {
Public $ value = 0;
Public function & getValue (){
Return $ this-> value;
}
}
$ Obj = new foo;
$ MyValue = & $ obj-> getValue (); // $ myValue is a reference to $ obj-> value, which is 42.
$ Obj-> value = 2;
Echo $ myValue;
Some simple examples
Let's take a look at the simple example below and try to understand the reference return.
Function & test ()
{
// Declare a static variable
Static $ B = 0;
$ B = $ B + 1;
Echo $ B;
Return $ B;
}
$ A = test (); // This statement outputs the value of $ B as 1.
$ A = 5;
$ A = test (); // This statement outputs the value of $ B to 2.
$ A = & test (); // This statement outputs the value of $ B to 3.
$ A = 5;
$ A = test (); // This statement outputs a value of 6 for $ B.
?>
Program running result:
1
2
3
6
Although the function declaration method is function & test (), what we call the function $ a = test () in this way is not actually the function reference, this is no different from common function calls. PHP requires that $ a = & test () is used to obtain the function reference and return.
In the example above, $ a = test () is used to call a function. It only assigns the value of the function to $, any change made by $ a does not affect $ B in the function.
But how to call a function through $ a = & test, it directs the memory address of the $ B Variable in return $ B to the same place as the memory address of the $ a variable. That is, it produces the equivalent effect ($ a = & $ B), so changing the value of $ a also changes the value of $ B. Therefore
$ A = & test ();
$ A = 5;
Then, the value of $ B is changed to 5.
Let's look at another program example for better understanding:
/*
** Value transfer and reference transfer: the value passes a duplicate of the value, and the reference passes the memory address pointed to by the value.
*/
// Add &
Function & func ($ a, $ B ){
// Here, a static variable is defined to display the effect more intuitively.
Static $ result = 0;
$ Result + = $ a + $ B;
Echo $ result .'
';
Return $ result;
}
$ A = $ B = 10;
// Writing a function reference call in PHP is no different from calling a common function (just copying the return value of the function to the $ c variable, $ c does not affect the $ result in the above function)
// Remember: function reference definitions and calls in PHP must be added before the function name &
$ C = func ($ a, $ B );
// When func () is executed for the first time, the value of its static variable $ result is 20 (10 + 10)
// Changing the value of $ c does not affect the following statement.
$ C = 666;
// For the second execution of func (), the value of its static variable $ result is changed to 40 (20 + 10 + 10)
$ C = func ($ a, $ B );
Echo'
';
// This is the call Method for referencing functions in PHP.
$ D = & func ($ a, $ B );
// For the third execution of func (), the value of its static variable $ result is changed to 40 (40 + 10 + 10)
USD d = 888;
// For the fourth execution of func (), the value of its static variable $ result is changed to 908 (888 + 10 + 10)
$ D = func ($ a, $ B );
?>