探讨PHP引用&符号的一些注意问题
PHP引用&符号是一个比较难掌握的知识点。新手们在实际编写代码时一定要多注意这一点,因为如果你对PHP引用&符号的使用理解错误,将会导致你所编写的整个代码出现错误。
- 如何运用PHP关联数组查询结果
- PHP常见错误的具体纠正方法
- PHP函数header()查询单部件的具体方法解
- WordPress获得2009开源PHP项目亚军
- PHP数组转字符串与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%以上。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
