search
Homephp教程php手册探讨PHP引用&符号的一些注意问题

探讨PHP引用&符号的一些注意问题

Jun 13, 2016 am 10:19 AM
ampphpQuotemasterDiscussyesCompareNoticeofKnowledge pointssymbolquestion

PHP引用&符号是一个比较难掌握的知识点。新手们在实际编写代码时一定要多注意这一点,因为如果你对PHP引用&符号的使用理解错误,将会导致你所编写的整个代码出现错误。

  • 如何运用PHP关联数组查询结果
  • PHP常见错误的具体纠正方法
  • PHP函数header()查询单部件的具体方法解
  • WordPress获得2009开源PHP项目亚军
  • PHP数组转字符串与PHP字符串转数组的相关
很多人误解php中的引用跟C当中的指针一样,事实上并非如此,而且很大差别。C语言中的指针除了在数组传递过程中不用显式申明外,其他都需要使用*进行定义,而php中对于地址的指向类似指针)功能不是由用户自己来实现的,是由Zend核心实现的,PHP引用&符号采用的是“写时拷贝”的原理,就是除非发生写操作,指向同一个地址的变量或者对象是不会被拷贝的,比如下面的代码:

<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>

如果程序仅执行到这里,$b和$b是相同的,但是并没有像C那样,$a和$b占用不同的内存空间,而是指向了同一块内存,这就是php和c的差别,并不需要写成$b=&$a才表示$b指向$a的内存,zend就已经帮你实现了引用,并且zend会非常智能的帮你去判断什么时候该这样处理,什么时候不该这样处理。

如果在后面继续写如下代码,增加一个函数,通过PHP引用&符号的方式传递参数,并打印输出数组大小。

<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>

上面的代码中,我们通过PHP引用&符号把$a数组传入printArray()函数,zend引擎会认为printArray()可能会导致对$a的改变,此时就会自动为$b生产一个$a的数据拷贝,重新申请一块内存进行存储。这就是前面提到的“写时拷贝”概念。

如果我们把上面的代码改成下面这样:

<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>


上面的代码直接传递$a值到printArray()中,此时并不存在引用传递,所以没有出现写时拷贝。

大家可以测试一下上面两行代码的执行效率,比如外面加入一个循环1000次,看看运行的耗时,结果会让你知道不正确使用PHP引用&符号会导致性能下降30%以上。

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function