search
HomeBackend DevelopmentPHP Tutorial 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();的话就会出错

???

说明参数只能是变量,常量不具有传址。

?

函数的引用返回

函数的引用返回多用在对象中,这里方便理解用静态变量做个例子

例四:

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的数据拷贝,重新申请一块内存进行存储。

?

?

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
Explain the concept of session locking.Explain the concept of session locking.Apr 29, 2025 am 12:39 AM

Sessionlockingisatechniqueusedtoensureauser'ssessionremainsexclusivetooneuseratatime.Itiscrucialforpreventingdatacorruptionandsecuritybreachesinmulti-userapplications.Sessionlockingisimplementedusingserver-sidelockingmechanisms,suchasReentrantLockinJ

Are there any alternatives to PHP sessions?Are there any alternatives to PHP sessions?Apr 29, 2025 am 12:36 AM

Alternatives to PHP sessions include Cookies, Token-based Authentication, Database-based Sessions, and Redis/Memcached. 1.Cookies manage sessions by storing data on the client, which is simple but low in security. 2.Token-based Authentication uses tokens to verify users, which is highly secure but requires additional logic. 3.Database-basedSessions stores data in the database, which has good scalability but may affect performance. 4. Redis/Memcached uses distributed cache to improve performance and scalability, but requires additional matching

What is the full form of PHP?What is the full form of PHP?Apr 28, 2025 pm 04:58 PM

The article discusses PHP, detailing its full form, main uses in web development, comparison with Python and Java, and its ease of learning for beginners.

How does PHP handle form data?How does PHP handle form data?Apr 28, 2025 pm 04:57 PM

PHP handles form data using $\_POST and $\_GET superglobals, with security ensured through validation, sanitization, and secure database interactions.

What is the difference between PHP and ASP.NET?What is the difference between PHP and ASP.NET?Apr 28, 2025 pm 04:56 PM

The article compares PHP and ASP.NET, focusing on their suitability for large-scale web applications, performance differences, and security features. Both are viable for large projects, but PHP is open-source and platform-independent, while ASP.NET,

Is PHP a case-sensitive language?Is PHP a case-sensitive language?Apr 28, 2025 pm 04:55 PM

PHP's case sensitivity varies: functions are insensitive, while variables and classes are sensitive. Best practices include consistent naming and using case-insensitive functions for comparisons.

How do you redirect a page in PHP?How do you redirect a page in PHP?Apr 28, 2025 pm 04:54 PM

The article discusses various methods for page redirection in PHP, focusing on the header() function and addressing common issues like "headers already sent" errors.

Explain type hinting in PHPExplain type hinting in PHPApr 28, 2025 pm 04:52 PM

Article discusses type hinting in PHP, a feature for specifying expected data types in functions. Main issue is improving code quality and readability through type enforcement.

See all articles

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

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

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.