Example of PHP quote & Usage

Source: Internet
Author: User

Php reference is to add the & symbol before variables, functions, and objects. The reference in PHP means that different names access the same variable content. The pointer is different from the pointer in the C language. The pointer in the C language stores the address where the variable content is stored in the memory.

Variable reference

PHP references allow you to use two variables to point to the same content.

<? $ 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?>
Function address transfer call

I won't say much about the address transfer call. The code is provided below:

Function 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.

Function reference return

First look at the Code:

Function & 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 a value of 6 for $ B.

In this way, $ a = test (); is not actually returned by the function reference, which is no different from the normal function call. As for the reason: this is a 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 .)

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, 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.

$a=&test(); $a=5; 

Then, 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
<? Class 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?>

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.

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 $.

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 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 when writing, unless a write operation occurs, the variables or objects pointing to the same address will not be copied.

In general, if the following code is available:

$a="ABC"; $b=$a; 

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

If the following code is added on the basis of the above Code:

$a="EFG";

Because the memory data pointed to by $ a and $ B needs 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.