Usage and precautions of PHP reference operators & amp;

Source: Internet
Author: User
Use and note of PHP reference operators & amp; note: PHP reference operators &, anyone familiar with C knows that C has something called a pointer, which points to the memory address. This & has the same function.

See the following code:

$source="110";$a=$source;$b=&$source;$source="120";echo $a."\r\n",$b;

After this code is run, you will find a problem. The value of $ a does not change according to the value of $ source in the fourth line of code. the original value is still "110". This is easy to understand, when $ a is assigned a value, the value of $ source is 110. she just copied the value of $ source to herself.

Obviously, you will also notice that the value of the variable $ B has changed. $ a and the variable $ B are almost identical values, why is there such a huge difference? there is a big difference between a person in charge and a person in charge!

This is the PHP reference operator & Problem. the variable $ B is applied to the variable $ B during the assignment, and $ B is not copied to itself as "110, it directly points to the old nest of $ source. in the future, $ source will be his $ B. $ Source will change $ B no matter how it changes. it is like the relationship between a host and two monitors. because of this, changes in $ B will certainly lead to changes in $ source.

See:

$b=122;echo $source;

Output result: 122. you know, these two variables are now "people". don't bully them!

In fact, for the readability of the program and subsequent programming misoperations, I do not recommend using this & reference operator, you think. You used a $ B = & $ source before row 10000, and you don't have to remember it after row 10000. in case you accidentally assigned the wrong value, it's enough for you to have a pot of tea! Haha ......

In fact, this operator is mostly used for database connection, because when we create a database connection object, we usually only need one, too much is useless.

Suppose we have a class:

Class MysqlConnect {} // used to create a database connection, so we can write $ conn = & new MysqlConnect () every time we call it ();

This statement ensures that database connections are not repeatedly created and system resources are consumed. But if you really need multiple different connections, do not write them like this.

Of course, this PHP reference operator is indeed useful when you create objects. if you create thousands of objects in a PHP script program, the system overhead is indeed very high. If you do not need to create multiple instances, try to use them!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.