php引述&符号详解
php引用&符号详解
php的引用(就是在变量或者函数、对象等前面加上&符号)
在PHP中引用的意思是:不同的名字访问同一个变量内容.
?
变量的引用
PHP的引用允许你用两个变量来指向同一个内容
例一:
$a="2010";
$b =&$a;
echo $a;//这里输出:2010
echo $b;//这里输出:2010
$b="2012";
echo $a;//这里$a的值变为2012所以输出
echo $b;//这里输出2012
?>
例二:
$a = "date";
$b = &$a;
echo $a; // date
echo $b; // date
$b = "date1";
echo $a; // date1
echo $b; // date1
unset($a);
echo $b; // date1
?>
从上面的两个例子中,可以看出,把$b的内存地址给了$b,并不是简单的赋值。所以对$b
的任何操作也会影响到$a
另种说法,就是给$a增加了一个别名$b,如果删除了$a,只是删除了这个变量的名字,并没有删除变量的内容,用别名还是可以把这个变量的内容显示出来。(如图关系)
???????
函数的传址调用
例三:
function test(&$a)
{
$a=$a+100;
}
$b=1;
echo $b;//输出1
//这里$b传递给函数的其实是$b的变量内容所处的内存地址,通过在函数里改变$a的值 就可以改变$b的值了
test($b);???
echo $b;//输出101
?>
如何在这里test(1);的话就会出错
???
说明参数只能是变量,常量不具有传址。
?
函数的引用返回
函数的引用返回多用在对象中,这里方便理解用静态变量做个例子
例四:
function &test()
{
????static $b=0;//申明一个静态变量
????$b=$b+1;
????echo $b;
????return $b;
}
//这条语句会输出 $b的值 为1
$a=test();
$a=5;
$a=test();//这条语句会输出 $b的值 为2
?
$a=&test();//这条语句会输出 $b的值 为3
$a=5;
$a=test();//这条语句会输出 $b的值 为6
注释,这个函数是有输出的,而且也有返回值的。
$a = test();只是将函数test的返回值$b赋给$a了,就是很普通的赋值而已,不是函数的引用返回。所以$a不管做什么操作,都不会影响$b。
$a = &test();作用就是将$b的内存地址与$a的内存地址指向了同一个地方,会产生类似于$b = &$a的效果,如果$a的值改变了,即变成了5,也会影响$b的值。所以在执行$a = &test();$a = 5,就有$b = 5,经过函数处理输出$b = 6;
?>
?
对象的引用
例五:
class a{
var $abc="ABC";
}
$b=new a;
$c=$b;
echo $b->abc;//这里输出ABC
echo $c->abc;//这里输出ABC
$b->abc="DEF";
echo $c->abc;//这里输出DEF
?>
以上代码是在PHP5中的运行的效果,在PHP5中对象的复制是通过引用来实现的。
上列中$b=new a; $c=$b;其实等效于$b=new a; $c=&$b;
PHP5中默认就是通过引用来调用对象,但有时你可能想建立一个对象的副本,并希望原来的对象的改变不影响到副本。为了这样的目的,PHP定义了一个特殊的方法,称为__clone。
?
引用的作用
如果程序比较大,引用同一个对象的变量比较多,并且希望用完该对象后手工清除它,建议用"&"方式,然后用$var=null的方式清除。其它时候还是用php5的默认方式吧.。
另外, php5中对于大数组的传递,建议用"&"方式,毕竟节省内存空间使用。
?
取消引用
当你unset一个引用,只是断开了变量名和变量内容之间的绑定。这并不意味着变量内容被销毁了。
例如:
?
$a = 1;
$b =& $a;
unset ($a);
?>??
不会unset $b,只是$a。
可以参看变量的引用那段
?
?global 引用
当用global $var声明一个变量时实际上建立了一个到全局变量的引用。
它等价于下面这段代码:
$var =& $GLOBALS["var"];
?>?
?这意味着,例如,unset $var不会unset全局变量。
?
$this
在一个对象的方法中,$this永远是调用它的对象的引用。
?
另外说明
php中对于地址的指向(类似指针)功能不是由用户自己来实现的,是由Zend核心实现的,php中引用采用的是“写时拷贝”的原理,就是除非发生写操作,才会拷贝,其他操作,指向同一个地址的变量或者对象是不会被拷贝的。
假如,有以下代码:
$a="ABC";
$b=$a;
Ps:我个人认为这里应该是$b = &$a,才能使$a和$b指向同一内存地址,但是我参考的资料上面就是这么写的,目前我对&了解的还不是很深入,如果有朋友有不同见解的可以提出来,谢谢喽
此时,$a与$b都是指向同一内存地址,而并不是$a与$b占用不同的内存
?
如果在上面的代码基础上再加上,如下代码
$a="EFG";
这里进行“写”操作了
由于$a与$b所指向的内存的数据要重新写一次了,此时Zend核心会自动判断,自动为$b生产一个$a的数据拷贝,重新申请一块内存进行存储。
?
?

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



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

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

Huawei is rolling out software version 5.0.0.100(C00M01) for the Watch GT 5 and the Watch GT 5 Prosmartwatchesglobally. These two smartwatches recently launched in Europe, with the standard model arriving as the company’s cheapest model. This Harmony

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.
