Home >Backend Development >PHP Tutorial >Discuss some issues to note when referencing the & symbol in PHP_PHP Tutorial

Discuss some issues to note when referencing the & symbol in PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-15 13:29:29855browse

Many people misunderstand that references in PHP are the same as pointers in C. In fact, they are not, and they are very different. Except for the pointers in C language that do not need to be explicitly declared during the array transfer process, other points need to be defined using *. However, the pointer to address (similar to a pointer) function in PHP is not implemented by the user himself, but is implemented by the Zend core. Yes, the PHP reference & symbol adopts the principle of "copy-on-write", that is, unless a write operation occurs, variables or objects pointing to the same address will not be copied, such as the following code:

<ol class="dp-xml">
<li class="alt"><span><span>$</span><span class="attribute"><font color="#ff0000">a</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">array</font></span><span>('a','c'...'n');  </span></span></li>
<li class="">
<span>$</span><span class="attribute"><font color="#ff0000">b</font></span><span> = $a; </span>
</li>
</ol>

If the program only executes to this point, $b and $b are the same, but not like C, $ a and $b occupy different memory spaces, but point to the same memory. This is the difference between php and c. It does not need to be written as $b=&$a to mean that $b points to the memory of $a. zend has already helped You implement the reference, and zend will very intelligently help you judge when it should be handled this way and when it should not be handled like this.

If you continue to write the following code later, add a function, pass the parameters by using the & symbol in PHP, and print out the array size.

<ol class="dp-xml">
<li class="alt"><span><span>function printArray(&$arr) //引用传递  </span></span></li>
<li class=""><span>    {  </span></li>
<li class="alt"><span>        print(count($arr));  </span></li>
<li class=""><span>    }  </span></li>
<li class="alt"><span> </span></li>
<li class=""><span>     printArray($a); </span></li>
</ol>

In the above code, we pass the $a array into the printArray() function through the PHP reference & symbol, zend The engine will think that printArray() may cause changes to $a. At this time, it will automatically produce a data copy of $a for $b and re-apply for a piece of memory for storage. This is the "copy-on-write" concept mentioned earlier.

If we change the above code to the following:

<ol class="dp-xml">
<li class="alt"><span><span>function printArray($arr)  //值传递  </span></span></li>
<li class=""><span>    {  </span></li>
<li class="alt"><span>        print(count($arr));  </span></li>
<li class=""><span>    }  </span></li>
<li class="alt"><span> </span></li>
<li class=""><span>     printArray($a); </span></li>
</ol>


The above code directly passes the $a value to printArray(). There is no reference transfer at this time, so there is no copy-on-write.

You can test the execution efficiency of the above two lines of code. For example, add a loop outside 1000 times and see how long it takes to run. The result will let you know that incorrect use of the PHP reference & symbol will cause a performance drop of 30 %above.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446367.htmlTechArticleMany people misunderstand that references in php are the same as pointers in C. In fact, they are not, and they are very different. Except for the pointers in C language that do not need to be explicitly declared during the array transfer process, other...
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