Php Reference & explanation

Source: Internet
Author: User

A simple & Symbol in php can be described in many articles. Today we will briefly talk about the usage of php variable reference and parameter value passing. I hope beginners can refer to this article.

In this way, $ a = test (); is not actually returned by the function reference, which is no different from the normal function call. The reason is: this is the PHP rule.
PHP requires that $ a = & test (); is used to obtain the function reference and return.
As for what is reference return (in the PHP manual, reference return is used when you want to use a function to find the variable on which the reference should be bound .) I haven't understood this sentence for a long time.

The example above is as follows:
$ A = test () is used to call a function. It only assigns the value of the function to $ a. Any change made to $ a does not affect $ B in the function.
But how to call a function through $ a = & test, the function is to direct the memory address of the $ B Variable in return $ B to the same place as the memory address of the $ a variable.
That is, the equivalent effect ($ a = & B;) is generated. Therefore, changing the value of $ a also changes the value of $ B.

The Code is as follows: Copy code
$ A = & test ();
$ A = 5;

Later, the value of $ B is changed to 5.

Static variables are used to help you understand the reference and return functions. In fact, function reference and return are mostly used in objects.

Object Reference

The above code is the running effect in PHP5
In PHP5, object replication is implemented through reference. In the above column, $ B = new a; $ c = $ B; is equivalent to $ B = new a; $ c = & $ B;
In PHP5, the object is called by reference by default, but sometimes you may want to create a copy of the object and expect that the change of the original object will not affect the copy. for this purpose, PHP defines a special method called _ clone.

Role of reference
If the program is large, there are many variables that reference the same object, and you want to manually clear the object after it is used up, I suggest using the & method, then clear it in the form of $ var = null. in other cases, use the default php5 method. in addition, we recommend that you use the "&" Method for transferring large arrays in php5 to save memory space.

Cancel reference
When you unset a reference, you just disconnect the binding between the variable name and the variable content. This does not mean that the variable content is destroyed. For example:

The Code is as follows: Copy code

<? Php
$ A = 1;
$ B = & $;
Unset ($ );
?>

Not unset $ B, just $.

Global Reference
When a variable is declared with global $ var, a reference to the global variable is actually created. That is to say, it is the same as doing so:

The Code is as follows: Copy code

<? Php
$ Var = & $ GLOBALS ["var"];
?>

This means that, for example, unset $ var does not unset global variables.

$ This
In the method of an object, $ this is always a reference to the object that calls it.

// Next is an episode
In php, the address pointing (similar to pointer) function is not implemented by the user, but is implemented by the Zend core. in php, the reference uses the principle of "Copy at write, unless a write operation occurs, the variables or objects pointing to the same address will not be copied.

In layman's terms
1: If the following code exists:

In fact, both $ a and $ B point to the same memory address, not $ a and $ B occupy different memory.

The Code is as follows: Copy code

$ Source = "110 ";
$ A = $ source;
$ B = & $ source;
$ Source = "120 ";
Echo $ a. "rn", $ B;

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.

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.

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:

The Code is as follows: Copy code

Class MysqlConnect {} // used to create a database connection, so we can write
 
$ Conn = & new MysqlConnect ();

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.

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.