PHP Reference Operators & The use and Precautions

Source: Internet
Author: User
PHP reference operator &, familiar with C know that there is a thing called pointer in C, the pointer is pointing to the memory address. This & also has the same function.

Look at the following code:

$source = "", $a = $source, $b =& $source; $source = "+"; Echo $a. " \ r \ n ", $b;

After this code runs you will find a problem, $a value is not changed according to the value of the fourth line of code $source change is still the original assignment "110", this very good understanding, $a assignment value $source value is 110, she just put $ The value of source copy one gives itself.

Obviously you will also notice that the value of variable $b has changed, $a and variable $b almost simultaneous assignment, why there is such a huge difference--a tube hit a tube to save people, the difference is big enough oh!

This is the PHP reference operator & problem, variable $b because in the assignment when applied to & cause, $b not copy "110" to himself but, direct point to $source's lair, later $source is he $b. $source no matter how the change will lead to $b changes-much like a host connection two monitors relationship, since it is such a relationship $b change of course will lead to $source changes

See:

$b =122;echo $source;

Output results: 122, you know, these two variables are a "people", do not bully them Oh!

In fact, for the readability of the program and subsequent programming of the misoperation, I do not recommend the use of this & reference operator, you want AH. You used a $b=& $source before line 10000, and after 10000 lines you may not remember, in case you accidentally assigned the wrong value, the wrong time is enough for you to drink a pot! Oh......

In fact, this operator is more used in the database connection, because we create database connection object, often only need one, too much useless ah.

Suppose we have a class:

Class mysqlconnect{}//is used to create a database connection, so we can write each time we call  $conn =& new Mysqlconnect ();

This way of writing can ensure that the database connection will not be created repeatedly, consuming system resources. But if you really need more than one connection, you mustn't write like this.

Of course, this PHP reference operator is really useful when creating objects, and if you create thousands of objects on a PHP script, the overhead of the system is really great. If there is no need to create multiple, try to use &!

  • Related Article

    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.