Php quote & symbol explanation

Source: Internet
Author: User
Php Reference & amp; symbol details php reference (that is, add the & amp; symbol before variables, functions, objects, etc.) in PHP reference: different names access the same variable content .? The reference of the variable PHP allows you to use two variables to point to the same content. Example 1: & lt ;? Php $ a = "2010"; $ B = & amp; $ a php Reference & symbol details

Php reference (that is, adding & symbol before variables, functions, and objects)

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

Example 1:

$ A = "2010 ";

$ B = & $;

Echo $ a; // output here: 2010

Echo $ B; // output here: 2010

$ B = "2012 ";

Echo $ a; // Here, the value of $ a is changed to 2012, so the output is

Echo $ B; // output 2012

?>

Example 2:

$ A = "date ";

$ B = & $;

Echo $ a; // date

Echo $ B; // date

$ B = "date1 ";

Echo $ a; // date1

Echo $ B; // date1

Unset ($ );

Echo $ B; // date1

?>

From the two examples above, we can see that it is not a simple assignment to assign the memory address of $ B to $ B. So for $ B

Any operation will also affect $

Another way is to add an alias $ B to $ a. If $ a is deleted, the variable name is deleted and the variable content is not deleted, you can use an alias to display the variable content. (Link)

???????

Function address transfer call

Example 3:

Function test (& $)

{

$ A = $ a + 100;

}

$ B = 1;

Echo $ B; // output 1

// Here $ B is actually the memory address of the variable content of $ B. you can change the value of $ B by changing the value of $ a in the function.

Test ($ B );???

Echo $ B; // output 101

?>

Test (1 );

???

It indicates that the parameter can only be a variable and the constant does not have a transfer address.

?

Function Reference return

Function Reference and return are mostly used in objects. here we can easily understand static variables as an example.

Example 4:

Function & test ()

{

???? Static $ B = 0; // declare a static variable

???? $ B = $ B + 1;

???? Echo $ B;

???? Return $ B;

}

// This statement outputs the value of $ B as 1.

$ A = test ();

$ A = 5;

$ A = test (); // This statement outputs the value of $ B to 2.

?

$ A = & test (); // This statement outputs the value of $ B to 3.

$ A = 5;

$ A = test (); // This statement outputs a value of 6 for $ B.

Note: This function has output and return values.

$ A = test (); it only assigns the return value of function test $ B to $ a, which is a normal value, not a function reference. Therefore, no matter what you do, $ a does not affect $ B.

$ A = & test (); the function is to direct the memory address of $ B to the memory address of $ a, which produces effects similar to $ B = & $, if the value of $ a changes to 5, the value of $ B is also affected. Therefore, when $ a = & test (); $ a = 5 is executed, $ B = 5 is generated, and $ B = 6 is output after function processing;

?>

?

Object reference

Example 5:

Class {

Var $ abc = "ABC ";

}

$ B = new;

$ C = $ B;

Echo $ B-> abc; // output ABC here

Echo $ c-> abc; // output ABC here

$ B-> abc = "DEF ";

Echo $ c-> abc; // output DEF here

?>

The above code is the effect of running 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, we recommend that you use 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:

?

$ A = 1;

$ B = & $;

Unset ($ );

?> ??

Not unset $ B, just $.

See the reference section of the variable.

?

?GlobalReference

When a variable is declared with global $ var, a reference to the global variable is actually created.

It is equivalent to the following code:

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

?

Additional instructions

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, the copy operation is performed only when a write operation occurs. other operations, variables or objects pointing to the same address, are not copied.

Assume that you have the following code:

$ A = "ABC ";
$ B = $;

Ps: I personally think it should be $ B = & $ a to Point $ a and $ B to the same memory address, but this is what I wrote above for reference, at present, I am not very familiar with it. if you have some friends who have different opinions, you can give me some advice. thank you!

In this case, both $ a and $ B point to the same memory address, instead of occupying different memory resources for $ a and $ B.

?

If you add the following code based on the above code:

$ A = "EFG ";

Here, the "write" operation is performed.

Because the memory data pointed to by $ a and $ B needs to be re-written, the Zend core will automatically determine and automatically generate a $ a data copy for $ B, apply for a new memory for storage.

?

?

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.