Home  >  Article  >  Backend Development  >  The role of the & symbol in php - variable reference, function reference, object reference

The role of the & symbol in php - variable reference, function reference, object reference

巴扎黑
巴扎黑Original
2017-08-05 10:22:331769browse

PHP reference (that is, adding the & symbol in front of a variable, function, object, etc.) //The most important thing is to delete the referenced variable. It is just that the referenced variable cannot be accessed, but the content is not destroyed. What it means to reference in PHP is : Different names access the same variable content.

Variable reference
    
PHP reference allows you to use two variables to point to the same content


<?php$a="ABC"; 
$b =&$a; 
echo $a;//这里输出:ABC echo $b;//这里输出:ABC $b="EFG"; 
echo $a;//这里$a的值变为EFG 所以输出EFG echo $b;//这里输出EFG ?>

I won’t go into details about the call-by-reference function and the call-by-reference function. The code will be given directly below


<?phpfunction test(&$a){ 
    $a=$a+100; 
} 
$b=1; 
echo $b;//输出1 test($b);   //这里$b传递给函数的其实是$b的变量内容所处的内存地址,通过在函数里改变$a的值 就可以改变$b的值了 echo "<br>"; echo $b;//输出101?>


It should be noted that , if test(1); is used here, an error will occur. The reason is: PHP stipulates that the passed reference cannot be a constant (see the error message).

Function reference return Let’s look at the code first


<?phpfunction &test(){ 
    static $b=0;//申明一个静态变量 
    $b=$b+1; 
    echo $b; 
    return $b; }
}$a=test();//这条语句会输出 $b的值 为1 $a=5; $a=test();//这条语句会输出 $b的值 为2$a=&test();//这条语句会输出 $b的值 为3 $a=5; $a=test();//这条语句会输出 $b的值 为6?>


Explain below: In this way $a=test (); actually does not get a function reference return, which is no different from an ordinary function call. The reason: This is the regulation of PHP. PHP stipulates that what is obtained through $a=&test(); is a function reference return. As for what is What about return by reference (the PHP manual says: Return by reference is used when you want to use a function to find which variable the reference should be bound to.) This bullshit kept me from understanding it for a long time

Use the above An example to explain is that calling a function using $a=test() only assigns the value of the function to $a, and any changes to $a will not affect $b in the function. When calling a function through $a=&test(), its function is to point the memory address of the $b variable in return $b and the memory address of the $a variable to the same place, which is equivalent to Such an effect ($a=&b;) So changing the value of $a also changes the value of $b, so after executing $a=&test(); $a=5;, $b The value has changed to 5

The static variable is used here to let everyone understand the reference return of the function. In fact, the reference return of the function is mostly used in the object

Reference of the object


##

<?phpclass a{    var $abc="ABC";
} 
$b=new a; 
$c=$b; 
echo $b->abc;//这里输出ABC echo $c->abc;//这里输出ABC $b->abc="DEF"; echo $c->abc;//这里输出DEF?>
The above code is the running effect in PHP5

In PHP5, the copying of objects is achieved through references . In the above column, $b=new a; $c=$b; is actually equivalent to $b=new a; $c=&$b; The default in PHP5 is to call the object by reference, but sometimes you may want to create an object A copy of the original object and hope that changes to the original object will not affect the copy. For this purpose, PHP defines a special method called __clone.

The role of reference If the program is relatively large, there are many variables referencing the same object, and you want to manually clear the object after using it, I personally recommend using the "&" method, and then using $var=null to clear it. Other times, use php5. The default method. In addition, for the transfer of large arrays in php5, it is recommended to use the "&" method, after all, it saves memory space.

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

will not unset $b, just $a. function quoteTest(){ global $var; //Equivalent to $var = &$GLOBALS['var']; unset($var); //Deletion only deletes the reference, but the referenced content still exists. Same as above. This does not mean The variable content is destroyed}$var=1;quoteTest();echo $var; // Result 1

-------------------------- -------------------------------------------------- --------------------------

Will not unset $b, just $a.

function quoteTest(){ global $var; //Equivalent to $var = &$GLOBALS['var']; $var = 5; //Because they both point to the same memory content}$var=1 ;quoteTest();echo $var; //Result 5--------------------------------------------- -------------------------------------------------- -------

'&' This is a reference

global reference When declaring a variable with global $var, it actually creates a global variable. citation. That is the same as doing:

This means, for example, unset $var Global variables will not be unset.

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

//Here’s another little episode. The pointing (similar to pointer) function of the address in PHP is not implemented by the user himself, but is implemented by the Zend core. The reference in PHP uses "copy-on-write" The principle is that unless a write operation occurs, variables or objects pointing to the same address will not be copied.

Popular explanation 1: If there is the following code [php] $a="ABC"; $b=$a; [/php] In fact, $a and $b both point to the same memory address at this time It’s not that $a and $b occupy different memory

2: If you add the following code to the above code [php] $a="EFG"; [/php] Since the data in the memory pointed to by $a and $b will be rewritten, this At this time, the Zend core will automatically determine and automatically produce a data copy of $a for $b, and re-apply for a piece of memory for storage

The above is the detailed content of The role of the & symbol in php - variable reference, function reference, object reference. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn