PHP quote & usage details

Source: Internet
Author: User
The following describes the usage of the quote & amp; in PHP. If you need a friend, you can refer to the role of php reference (that is, adding & symbol before variables, functions, objects, etc.). let's take a look at the following program.

The code is as follows:


$ A = 100; // declare variable
$ B = & $ a; // declare variable B, reference independent variable
Echo "$
";
Echo "$ B
";
$ A ++; // variable a auto-Increment 1
Echo "$
";
Echo "$ B
"; // Check the variable B. also, 1 is added, indicating that the same storage unit is used.
?>


Program running result:

The code is as follows:


100
100
101
101


Many people misunderstand that the reference in php is the same as the pointer in C. in fact, this is not the case and it is quite different. In C language, pointers do not need explicit declarations in addition to the array transfer process, but must be defined by *. in php, pointers to addresses (similar to pointers) the function 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, variables or objects pointing to the same address are not copied.

Php transfers values by default:

The code is as follows:


$ A = 20;
$ B = $;
$ A = $ a + 10;
Echo $ a. 'and'. $ B;
?>


Program running result:

The code is as follows:


30 and 20


If you want to change to address transfer, you need to add &, both:

The code is as follows:


$ A = 20;
$ B = & $;
$ A = $ a + 10;
Echo $ a. 'and'. $ B;
?>


Program running result:

The code is as follows:


30 and 30


That is to say, the address of $ a is passed to $ B. In this case, the two variables now share a memory storage region, that is, their values are the same.

The same syntax can be used in functions, which return references, and in the new operator:

The code is as follows:


$ Bar = & new fooclass ();
$ Foo = & find_var ($ bar );
?>


The second thing to do with referencing is to use referencing to pass variables. This is achieved by creating a local variable in the function and referencing the same content in the call range. The common point is that a function parameter is a reference to a local variable. The following is an example:

The code is as follows:


Function foo (& $ val1, $ val2 ){
$ Val1 + = 1;
$ Val2 + = 1;
}
$ A = 5;
$ B = 10;
Foo ($ a, $ B );
Echo $;
Echo $ B;
?>


Run this code to pass two parameters to the function. one is the content that references $ a and the other is the value of $ B. after executing this function, we find that the content of $ a has changed, the content of $ B remains unchanged.

The third usage of PHP reference is reference return, which is difficult to understand. Reference return is used when you want to use a function to find the variable on which the reference should be bound. When a reference is returned, this syntax is used: the simple point is the return of the referenced function. However, unlike parameter passing, the & symbol must be used in both the function definition and function reference. The following is an example:

The code is as follows:


Function & find_var ($ param)
{
/*... Code ...*/
Return $ found_var;
}
$ Foo = & find_var ($ bar );
$ Foo-> x = 2;
?>


In this example, assigning a value to $ foo is the return reference of the find_var function. Therefore, when assigning a value to $ foo-> x, it is to assign a value to the return reference of find_var, rather than a simple value.

The last usage of PHP reference is reference positioning. There are two main applications: one is global reference. when a variable is declared using global $ var, a reference to the global variable is actually created. That is, it is the same as $ var = & $ GLOBALS ["var. The other is $ this. in an object method, $ this is always a reference to the object that calls it.

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.