PHP function Value pass-through/address and reference usage

Source: Internet
Author: User
Tags php write

1. First to explain the noun.

In the process of value passing (passl-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 manipulation of the modulated function on the formal parameter affects the central melody function.

Note: The above Red letter section shows that the application is not to open space, is to open up space, but open space is used to store the real parametric address.

There are three ways to use references in 2.php.

①. Assigning a reference to a variable: $a = & $b

②. Reference parameter passing when a function call

1) Early PHP is the variable that passes the reference type through the & symbol when it is invoked, for example: Func (& $arg);

2) Later, the reference type parameter of the function is defined as requiring a function declaration, rather than: function func (& $arg);

Note: When a reference-type parameter is defined in a reference declaration, the run-time reference parameter pass is discarded and needs to be added allow_call_time_pass_reference in the php.ini to open .

③. The function returns a reference type, which is required to declare the function, add the & symbol before the function name, and, when invoked, use the reference assignment as follows:

function &func () {     return $a;} $a = func ();  This method of invocation does not get the reference value $a =& func (); This invocation is the reference value.

Take a look at the following detailed examples:

$a = 1; Function &func (& $a) {   

  

3.php various data type values/pointers

First, the value of the basic data type

/* **************************************************** */  function TestVar ($k) {  $k = +;} $c = 30;// A basic data type (integer, Boolean, character ...) is passed to a function parameter, which is actually a value; TestVar ($c); echo $c;//The result is: function Testvar2 (& $k) {  $k = +;} $e = 30;//give a function parameter a basic data type (integer, Boolean, character ...), and actually the y is the address; Testva R2 ($e); echo $e;//result is: 40  

Second, the array (by default, a copy of the data), such as to address the & $arr

1 $arr 1=Array( -1,5,0); 2 functionTestarr ($arr){ 3   for($i= 0;$i<Count($arr);$i++){ 4    for($j=$i+1;$j<Count($arr);$j++){ 5    if($arr[$i]>$arr[$j]){ 6     $temp=$arr[$i]; 7     $arr[$i] =$arr[$j]; 8     $arr[$j] =$temp; 9    } Ten   }  One   A  }  -  Print_r($arr);//Result: Array ([0] =-1 [1] = 0 [2] = 5) - }  theTestarr ($arr 1);  - Print_r($arr 1);//Result: Array ([0] =-1 [1] = 5 [2] = 0) -   - functionTESTARR2 (&$arr){  +   for($i= 0;$i</Count><Count($arr);$i++){  -    for($j=$i+1;$j<Count($arr);$j++){  +    if($arr[$i]>$arr[$j]){  A     $temp=$arr[$i];  at     $arr[$i] =$arr[$j];  -     $arr[$j] =$temp;  -    }  -   }  -   -  }  in }  -Testarr ($arr 1);  to Print_r($arr 1);//Result: Array ([0] =-1 [1] = 0 [2] = 5)

Third, the object data type transmission value

Class person{public  $name;  Public  $age;}  $a = new person (); $a->name = ' Xiao Ming '; $a->age = ' 20 '; Variable A in the memory is the address of the object, assign a to the variable B, is actually assigned an address. $b = $a; $b->age = 30; echo $a->age. $b->age;//The result is: 30 30//Send an object to a function parameter, which actually passes the address of the object; function test ($k) {  

The replication of objects in PHP5 is done by reference. The above $a=new person; $b = $a; is actually equivalent to $a=new person; $b =& $a;
The default in PHP5 is to invoke the object by reference, but sometimes you might want to make a copy of the object, and you want the original object to change without affecting the copy. For this purpose, PHP defines a special method called __clone.

4.php Write-time copy

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.

The popular Speaking
1: If you have the following code

$a = "ABC";
$b = $a;

In fact, $ A and $b both point to the same memory address and not $ A and $b occupy different memory

2: If you add the following code based on the above code

$a = "EFG";

Since $ A and $b point to the memory of the data to be re-written once, at this time the Zend core will automatically decide to automatically produce a $ A copy of the data for $b, re-request a piece of memory for storage.

5.php Citation for C-pointer differences

The reference in PHP means: Different names access the same variable content.
There is a difference between the pointers in the C language. The pointer inside the C language stores the address where the contents of the variable are stored in memory.

PHP references allow you to use two variables to point to the same content

$a = "ABC"; $b =& $a, echo $a;//output here: Abcecho $b;//output here: abc$b= "EFG"; echo $a;//Here a value of $ A becomes EFG so the output efgecho $b;//Output EFG here

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:

unset ($a); echo $b;//Output EFG here

  

6. Ref:

Http://blog.sina.com.cn/s/blog_664c9f650101fl4b.html

Http://www.2cto.com/kf/201110/108970.html

Http://www.phpfensi.com/php/20140220/1612.html

PHP function Value pass-through/address and reference usage

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.