Detailed description of the value transfer and address transfer (reference) of the php function. In php, it is relatively simple to pass the function value, but some friends may not understand the transfer or reference of the innocent function, the following small series will introduce you to function data transfer and address transfer in php (it is easier to import function data transfer in php, however, some of my friends may not understand the transfer or reference of the innocent function. the following small series will introduce you to the introduction of function data transmission and address transfer (reference) in php. I hope this will help you.
Php reference usage:
1. variable reference value: $ a = & $ B
2. Transfer of reference parameters during function calling
1) in the early stage, php passed the reference type variables through the & symbol during the call, for example, func (& $ arg );
2) later, the parameter of the reference type of the function was defined as needed when the function was declared, rather than function func (& $ arg );
Note: after the reference type parameter is defined when the declaration is referenced, the reference parameter passing during runtime is discarded. you need to add allow_call_time_pass_reference in php. ini to enable it.
3. the function returns the reference type. when declaring a function, add the & symbol before the function name, and assign a value to the reference when calling the function. for example:
The code is as follows: |
|
Function & func (){ Return $; } $ A = func (); // This call method does not obtain the reference passed value. $ A = & func (); // This is the reference to pass the value
|
In short, let the function return a value of the reference type, a more practical example:
The code is as follows: |
|
$ A = 1; Function & func (& $ ){ Return $; } $ B = func ($ ); $ C = & func ($ ); $ B = 2; Echo "a: $ a, B: $ B, c: $ c. /N "; // Output a: 1, B: 2, c: 1. // Visible changes to $ B will not affect $ $ C = 3; Echo "a: $ a, B: $ B, c: $ c. /N "; // Output a: 3, B: 2, c: 3. // Visible changes to $ c will affect $ |
Several details about passing values in php functions
The code is as follows: |
|
// 1. transfer value of the basic data type /*************************************** ***************/ Function testvar ($ k ){ $ K = 40; } $ C = 30; // Pass a basic data type (integer, Boolean, character...) to a function parameter. the uploaded data is actually a value; Testvar ($ c ); Echo $ c; // The result is: 30 Function testvar2 (& $ k ){ $ K = 40; } $ E = 30; // Pass a basic data type (integer, Boolean, character...) to a function parameter. the actual uploaded y is the address; Testvar2 ($ e ); Echo $ e; // The result is: 40 /*************************************** ***************/ // 2. array (copying a copy of data by default). to transmit an address, & $ arr. $ Arr1 = array (-1, 5, 0 ); Function testArr ($ arr ){ For ($ I = 0; $ I For ($ j = $ I + 1; $ j If ($ arr [$ I]> $ arr [$ j]) { $ Temp = $ arr [$ I]; $ Arr [$ I] = $ arr [$ j]; $ Arr [$ j] = $ temp; } } } Print_r ($ arr); // result: Array ([0] =>-1 [1] => 0 [2] => 5) } TestArr ($ arr1 ); Print_r ($ arr1); // result: Array ([0] =>-1 [1] => 5 [2] => 0) Function testArr2 (& $ arr ){ For ($ I = 0; $ I For ($ j = $ I + 1; $ j If ($ arr [$ I]> $ arr [$ j]) { $ Temp = $ arr [$ I]; $ Arr [$ I] = $ arr [$ j]; $ Arr [$ j] = $ temp; } } } } TestArr ($ arr1 ); Print_r ($ arr1); // result: Array ([0] =>-1 [1] => 0 [2] => 5) /*************************************** ***************/ // 3. object data type value transfer Class person { Public $ name; Public $ age; } $ A = new person (); $ A-> name = 'xiaoming '; $ A-> age = '20 '; // Variable a stores the object address. assigning a to variable B actually assigns an address. $ B = $; $ B-> age = 30; // Echo $ a-> age. $ B-> age; // The result is: 30 30. // Upload an object to a function parameter. the address of the object is actually uploaded; Function test ($ k ){ $ K-> age = 40; } // Call Test ($ B ); // Echo $ a-> age. $ B-> age; // The result is: 40
|
What is the difference between the pass-through and transfer-through of php functions? For such a question, I like to use a program demonstration to obtain the answer. Let's look at a demo record!
The code is as follows: |
|
USD I = 100; Function func ($ n ){ $ N = $ n + 100; Return $ n; } Echo '1) the value of $ I before the function is passed: '. $ I .' '; Echo '2) return value of the function after passing the value: '. func ($ I ).' '; Echo '3) value of the variable $ I after the function is passed: '. $ I .' '; Echo '4) value of the variable $ I before the function address transfer: '. $ I .' '; Echo '5) return value of the function after the address is passed: '. func (& $ I ).' '; Echo '6) value of the variable $ I after the function is passed: '. $ I .' '; // Echo func ($ I ).' '; ?> |
Program output:
1) the variable $ I before the function is passed: 100
2) The return value of the function after passing the value: 200
3) value of the variable $ I after the function is passed: 100
4) value of $ I before function address transfer: 100
5) return value of the function after the address transfer: 200
6) value of the variable $ I after the function is transferred: 200
Explanation:
1) directly output a variable with a value of $ I = 100.
2) The func function returns an arithmetic addition result $ = 100 + 100
3) the function transfers values. the function is only used within the function, and the variable $ I outside will not be affected.
4) Same as above
5) There is an "&" symbol before the func function parameter, indicating that the address is transferred. like 2), the return value is the arithmetic running result of the function.
6) func (& $ I), the variable pointer points to the position of $ I, which can be understood as an operation on the variable $ I, $ I = $ I + 100; at this time, the variable $ I is equivalent to being assigned a value again.
What will be the result if the 18th line comment of the above code is removed?
Summary: In order to change the function parameter value while executing the function, the transfer value is not expected to change.
References (reference...