PHP value, pass-through reference

Source: Internet
Author: User

What is the difference between a PHP pass-through and a reference-to-pass address?

Pass Value:

is to assign the value of the argument to the parameter, then the modification of the parameter does not affect the value of the argument


Transfer address:

is a special way of passing a value, except that he passes an address, not an ordinary one, such as an int.
After the address is passed, both the argument and the parameter point to the same object

To pass a reference:

The real way to pass parameters in an address
After passing, the formal parameters and arguments are the same object, except that their names are different.
Modifying a parameter affects the value of an argument

Good understanding from the point of view of function invocation

Pass Value:
The function argument is a copy of the parameter, and any modification is a function on the copy, and it has no effect on the original variable.


Pass the pointer:
The stack is a copy of the pointer variable.
When you manipulate the pointer with the pointer, its value is pointing to the original variable, so it operates on the original variable.

To pass a reference:
The stack is a copy of the reference. Because a reference is a pointer to a variable, the operation of the reference is actually the operation of the variable that he points to. (function and pass the pointer, just do not have to dereference)

Basic theory of function parameter transfer mechanism


The problem of function parameter passing mechanism is essentially the method that calls the function (procedure) and the called function (procedure) to communicate when the call occurs; The purpose of the function is to deal with the target data (usually, other properties such as setting the value of the variable).

There are two basic parameter passing mechanisms: value passing and reference passing.

The following discussion calls functions that call other functions as the main function, and the function being called is the function of being tuned:

In the process of value passing (pass-by-value), the formal parameters of the called function are treated as local variables of the called function, that is, the memory space is opened up in the stack to hold the value of the arguments put in by the key function, thus becoming a copy of the argument. The characteristic of value passing is that any operation of the function on the formal parameter is done as a local variable, without affecting the value of the argument variable of the main key function.

In the process of reference passing (pass-by-reference), the formal parameters of the called function, while also opening up memory space in the stack as local variables, are stored in the address of the argument variable that is put in by the main key function. Any operation of the modulated function on the formal parameter is handled as an indirection, that is, the argument variable in the keynote function is accessed through the address stored in the stack. Because of this, any action taken by the modulated function on the parameter affects the argument variables in the keynote function.

As mentioned above, value passing and reference passing are not the same as the processing of argument variables, and the operation of a function on a value is not the same as a mechanism for a reference, and the parameter is always treated as a local variable, and the function is processed according to the copy of the value of the argument stored in its memory space or the address of the argument. As to how the function distinguishes values and addresses, I don't know, and I don't seem to have to.


Consider only value passing and referencing:

The so-called value passing means that only the value of the object is passed to the target object, which is equivalent to copy; The system will re-open an identical memory space for the target object.
The so-called reference, that is, to pass an object's in-memory address to the target object, is equivalent to making the target object and the original object correspond to the same memory storage space. In this case, the data in memory will change if the target object is modified.

The role of references
If the program is larger, referencing the same object is more variable, and you want to use the object after the manual removal of it, the personal suggestion "&" method, and then $var=null to clear. At other times, it's the default way to use PHP5. In addition, in the PHP5 for large arrays of delivery, it is recommended to use "&" mode, after all, saving memory space to use.

Dereference
When you unset a reference, you just break the binding between the variable name and the variable content. This does not mean that the contents of the variable are destroyed. For example:

<?php
$a = 1;
$b =& $a;
unset ($a);
?>

Not unset $b, just $a.

Global references
When you declare a variable with the global $var, you actually establish a reference to the global variable. In other words, it is the same as doing this:

<?php
$var =& $GLOBALS ["var"];
?>

This means, for example, that the unset $var does not unset global variables.

$this
In the method of an object, $this is always a reference to the object that called it.

Add:
PHP in the direction of the address (similar to the pointer) function is not implemented by the user itself, is implemented by the Zend Core, PHP refers to the use of "copy-on-write" principle, that is, unless a write operation, the same address to the variable or object is not copied.

If the value is a non-object, a copy of the value is passed, and any change to the variable does not affect the original value. A reference or an object is a real memory address, and changes made to that variable will affect the original value. function Func1 ($a) {  $a = $a + 1;} function Func2 (& $a) {  $a = $a + 1;} $sample = 1;func1 ($sample); Echo $sample; Output 1$sample = 1;FUNC2 ($sample); Echo $sample; Output 2

PHP value, pass-through reference

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.