What is the difference between passing a value in PHP, passing a reference, and passing an address?

Source: Internet
Author: User

 

Pass value,

Is to assign the value of the real parameter to the row Parameter

The modification of the row parameter does not affect the value of the real parameter.

 

Transfer address

It is a special method for transferring values, but it only transmits an address, not a common such as int

After the address is passed, both the real parameter and the row parameter point to the same object.

 

Upload reference

Actually passing parameters in address mode

After passing, the row parameters and real parameters are the same object, but their names are different.

Modifying the row parameter will affect the value of the real parameter.

 

Bytes -----------------------------------------------------------------------------------

 

I think it is better to understand function calls.

 

Pass value:

The parameter stack is a copy of the parameter.

Any modification works on the copy and does not work on the original variable.

 

Pass pointer:

The stack is a copy of the pointer variable.

When you perform pointer decoding, the value of the Pointer Points to the original variable. Therefore, you can perform operations on the original variable.

 

Reference:

The stack is a referenced copy. Because the reference is directed to a variable, the reference operation is actually the operation of the variable to which it points. (The function is the same as passing a pointer, but referencing a grass paper with little knowledge of the pointer)

 

Bytes -----------------------------------------------------------------------------------

Basic Theory of function parameter transfer mechanism

In essence, the function parameter transfer mechanism is a problem where the call function (process) and the called function (process) communicate when the call occurs. There are two basic parameter transfer mechanisms: value transfer and reference transfer. The following describes the functions that call other functions as the main functions. The called functions are called functions.

In the passl-by-value process, the formal parameters of the called function are processed as local variables of the called function, that is, the memory space is opened in the stack to store the values of the real parameters put in by the main function, thus becoming a copy of the real parameters. The feature of value transfer is that any operation of the form parameter of the called function is performed as a local variable, without affecting the value of the real parameter variable of the main function.

During the pass-by-reference process, the form parameter of the called function opens up the memory space in the stack as a local variable, however, the address of the Real Variable put in by the main function is stored. Any operation of the called function on the form parameter is processed as indirect addressing. That is, the address stored in the stack is used to access the real variable in the main function. Because of this, any operation performed by the called function on the form parameter affects

Real variable.

 

 

Bytes -----------------------------------------------------------------------------------

 

We will only discuss the value transfer and reference:

The so-called value transfer means that only passing the object value to the target object is equivalent to copy; the system will re-open a completely identical memory space for the target object.

The so-called reference means passing the address of the object in the memory to the target object is equivalent to making the target object and the original object correspond to the same memory bucket. At this time, if you modify the target object, the data in the memory will also change.

$ A = 'C ';

 

$ B = & $ a; // indicates that $ B and $ a reference the same variable.

 

$ A = 'abc'; // $ a is reset here.

 

Echo $ B; // returns abc

 

Unset ($ a); // cancel the reference

 

Echo $ B; // abc is still output here

 

$ A = 'abc ';

 

Echo $ B; // The abc is still output because the reference has been canceled.

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.