Detailed description of php function value transfer and address transfer (reference)

Source: Internet
Author: User

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 the introduction of Data Transmission Value and address transfer (reference) in php, and hope to 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: Copy code


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: Copy code

$ A = 1;
Function & func (& $ ){
Return $;
}
$ B = func ($ );
$ C = & func ($ );
$ B = 2;
Echo "a: $ a, B: $ B, c: $ c. <br/>/n ";
// Output a: 1, B: 2, c: 1.
// Visible changes to $ B will not affect $
$ C = 3;
Echo "a: $ a, B: $ B, c: $ c. <br/>/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: Copy code
// 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 <count ($ arr); $ I ++ ){
For ($ j = $ I + 1; $ j <count ($ arr); $ 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 </count> <count ($ arr); $ I ++ ){
For ($ j = $ I + 1; $ j <count ($ arr); $ 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
</Count>

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: Copy code
<? Php

USD I = 100;

Function func ($ n ){

$ N = $ n + 100;

Return $ n;

}
Echo '1) the value of $ I before the function is passed: '. $ I.' <br> ';
Echo '2) Return Value of the function after passing the value: '. func ($ I).' <br> ';
Echo '3) value of the variable $ I after the function is passed: '. $ I.' <br> ';
Echo '4) value of $ I before function Address Transfer: '. $ I.' <br> ';
Echo '5) Return Value of the function after the address is transferred: '. func (& $ I).' <br> ';
Echo '6) value of the variable $ I after the function is transferred: '. $ I.' <br> ';
// Echo func ($ I). '<br> ';
?>

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.