Php Reference & Usage Analysis [variable reference, function reference, object reference], php reference

Source: Internet
Author: User
Tags what php

Php Reference & Usage Analysis [variable reference, function reference, object reference], php reference

This article analyzes the reference & Usage in php. We will share this with you for your reference. The details are as follows:

Php references (that is, adding & symbol before variables, functions, and objects) // The most important thing is to delete the referenced variables, but the referenced variables cannot be accessed, but the content is not destroyed.

The reference in PHP means that different names access the same variable content.

Variable reference

PHP reference allows you to use two variables to point to the same content

<? Php $ a = "ABC"; $ B = & $ a; echo $ a; // output here: ABC echo $ B; // output here: ABC $ B = "EFG"; echo $ a; // here, the value of $ a is changed to EFG, so the EFG echo $ B is output. // EFG output here?>

I will not say much about the function's address transfer call. The following code is provided directly.

<? Phpfunction test (& $ a) {$ a = $ a + 100 ;}$ B = 1; echo $ B; // output 1 test ($ B ); // here $ B is actually the memory address of the variable content of $ B, by changing the value of $ a in the function, you can change the value of $ B by echo "<br>"; echo $ B; // output 101?>

Note that test (1); will cause an error because the reference passed by PHP cannot be a constant (you can see the error message ).

Function reference return first look at the code

<? Phpfunction & test () {static $ B = 0; // declare a static variable $ B = $ B + 1; echo $ B; return $ B ;}} $ a = test (); // This statement will output the value of $ B as 1 $ a = 5; $ a = test (); // This statement outputs the value of $ B as 2 $ a = & test (); // This statement outputs the value of $ B as 3 $ a = 5; $ a = test (); // This statement outputs the value of $ B as 6?>

The following explains: $ a = test (); in this way, the result is not actually a function reference and returned, which is no different from the normal function call. The reason is: this is what PHP requires to get through the $ a = & test (); method is the function reference return. What is the reference return? (in the PHP manual: the Return Value of the reference is used when you want to use the function to find the variable on which the reference should be bound .) I haven't understood this sentence for a long time.

In the example above, $ a = test () is used to call a function. It only assigns the value of the function to $ a, and $ a makes any changes, does not affect $ B in the function, but calls the function in the $ a = & test () method, his role is to direct the memory address of the $ B Variable in return $ B to the memory address of the $ a variable to the same place, which produces the equivalent effect ($ a = & B ;) therefore, changing the value of $ a also changes the value of $ B. Therefore, after $ a = & test (); $ a = 5; is executed, 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

<? Phpclass a {var $ abc = "ABC";} $ B = new a; $ c = $ B; echo $ B-> abc; // here ABC echo $ c-> abc; // here ABC $ B-> abc = "DEF"; echo $ c-> abc; // output DEF here?>

The above code is executed 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, however, sometimes you may want to create a copy of an 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.

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:

<?php $a = 1; $b =& $a; unset ($a); ?>

Not unset $ B, just $. Function quoteTest () {global $ var; // equivalent to $ var = & $ GLOBALS ['var']; unset ($ var); // Delete only deletes the reference, the referenced content still exists. The same as above does not mean that the variable content is destroyed} $ var = 1; quoteTest (); echo $ var; // result 1

Not unset $ B, just $.

Function quoteTest () {global $ var; // equivalent to $ var = & $ GLOBALS ['var']; $ var = 5; // because they all point to the same memory content} $ var = 1; quoteTest (); echo $ var; // result 5

'&' This is the reference

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:

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

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

$ This is always a reference to the object that calls an object in the method of an object.

In the next episode, the point-to-address (similar to pointer) function in php is not implemented by the user, but by the Zend core, the reference in php adopts the principle of "Copy at write time", that is, 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 [php] $ a = "ABC"; $ B = $; [/php] In fact, both $ a and $ B point to the same memory address, not that $ a and $ B occupy different memory.

2. add the following code [php] $ a = "EFG" based on the above Code "; [/php] Because $ a and $ B point to the memory data to be re-written, the Zend core automatically determines that $ B will generate a $ a data copy, apply for a new memory for storage

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.