PHP reference transfer and reference & usage introduction _ PHP Tutorial

Source: Internet
Author: User
PHP reference transfer and reference & amp; usage introduction. In php, the reference uses mdash; the variable name is a directory entry, and the variable content is the file itself. A reference can be considered as a hardlink in a Unix file system. The reference of PHP is used and used in php. next I will introduce some examples of usage and reference problems and issues in php. welcome to your reference.

Reference

Referencing in PHP means accessing the same variable content with different names. This is not like the pointer of C. Instead, the reference is the alias of the symbol table. Note that in PHP, the variable name and variable content are different, so the same content can have different names. The closest analogy is the Unix file name and the file itself-the variable name is a directory entry, while the variable content is the file itself. A reference can be considered as a hardlink in a Unix file system.

Reference
PHP references allow two variables to point to the same content.
When $ a = & $ B;, $ a and $ B point to the same variable.
Tip: $ a and $ B are exactly the same here. this doesn't mean $ a points to $ B or vice versa, but $ a and $ B point to the same place.

A variable can be passed to the function through reference, so that the function can modify the value of its parameter. Syntax:

The code is as follows:

Function foo (& $ var)
{
$ Var ++;
}

$ A = 5;
Foo ($ );
Echo $;

// The output is: 6


PHP quote &

For the role of php reference (that is, adding & symbol before variables, functions, and objects), let's first 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:

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:

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:

That is to say, & pass the $ a address 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:

View sourceprint?
1

2 $ bar = & new fooclass ();

3 $ foo = & find_var ($ bar );

4?>

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.


PHP references and misunderstandings


The reference in PHP can be understood as the alias of the variable. Because the PHP variable name is stored in the symbol table, the variable content is stored in the heap, and the reference is to use different symbols in the symbol table) name to access the same storage content, and the hardlink in the Unix file system is the same concept, for example:

The code is as follows:

$ A = 1;
$ B = & $ a; // $ a and $ B point to the same content
$ B = 2;
Echo $ B; // 2
Echo $ a; // 2
Pass reference
The reference transfer is very simple, it is a "&" symbol, for example:

Function foo (& $ ){
$ A = 2;
}

$ B = 1;
Foo ($ B );
Echo $ B; // 2

Return reference
In most cases, the zend Engine does not need to return a reference to improve performance. the zend Engine will optimize the performance by itself. However, if you have to return a reference, you can return the reference as follows:

The code is as follows:

Class foo {
Public $ value = 42;

Public function & getValue () {// A "&" is required "&"
Return $ this-> value;
}
}

$ Obj = new foo;
$ MyValue = & $ obj-> getValue (); // You also need a "&", $ myValue is a reference to $ value in class foo.
$ Obj-> value = 2; // modify the $ value attribute of an object
Echo $ myValue; // output 2. $ myValue is the same as $ value in class foo.

Difference from pointer
References are similar to pointers, but they are not pointers. See the following code:

The code is as follows:
$ A = 0;
$ B = &;
Echo $ a; // 0
Unset ($ B );
Echo $ a; // 0

Because $ B is only the alias of $ a, even if $ B is released, $ a has no effect, but the pointer is not like this. See the following code:

The code is as follows:

# Include
Int main (int argc, char const * argv []) {
Int a = 0;
Int * B = &;

Printf ("% in", a); // 0
Free (B );
Printf ("% in", a); // *** error for object 0x7fff6350da08: pointer being freed was not allocated
}

B is a pointer to a, so after B's memory is released, an error will occur when accessing a. It clearly shows the difference between PHP reference and C pointer.

Objects and references
When using objects in PHP, you are always told that "objects are passed by reference". In fact, this is a misunderstanding. PHP's object variable stores a identifier of this object. when passing an object, it actually passes this identifier instead of a reference. See the following code:

The code is as follows:

$ A = new;
$ B = $;
$ B-> testA = 2;

/*
* Relationships between $ a and $ B:
* + ----------- ++ ----------------- +
* $ A --> | object id | ---> | object (Class A) |
* + ----------- ++ ----------------- +
* ^
* + ----------- + |
* $ B --> | object id | --------- +
* + ----------- +
*
*
*/

$ C = new B;
$ A = $ c;
$ A-> testB = "Changed Class B ";

/*
* Relationships between $ a, $ B, and $ c:
* + ----------- ++ ----------------- +
* $ B --> | object id | ---> | object (Class A) |
* + ----------- ++ ----------------- +
*
* + ------------ +
* $ A --> | object id2 | ------------- +
* + ------------ + |
* V
* + ------------ ++ ----------------- +
* $ C --> | object id2 | ---> | object (Class B) |
* + ------------ ++ ----------------- +
*/

Echo "object a:"; var_dump ($ a); // ["testB"] => string (15) "Changed Class B"
Echo "object B:"; var_dump ($ B); // ["testA"] => int (2)
Echo "object c:"; var_dump ($ c); // ["testB"] => string (15) "Changed Class B"

If the object is passed by reference, the output content of $ a, $ B, and $ c should be the same. In fact, this is not the case. See the following example:

The code is as follows:

$ Aa = new;
$ Bb = & $ aa; // reference
$ Bb-> testA = 2;

/*
* Relationship between $ aa and $ bb:
*
* + ----------- ++ ----------------- +
* $ Bb --> | object id | ---> | object (Class A) |
* + ----------- ++ ----------------- +
* ^
* |
* $ Aa --------- +
*
*
*/

$ Cc = new B;
$ Aa = $ cc;
$ Aa-> testB = "Changed Class B ";

/*
* Relationships between $ aa, $ bb, and $ cc:
*
* + ----------- ++ ----------------- +
* | Object id | ---> | object (Class A) |
* + ----------- ++ ----------------- +
*
* $ Bb ----> ----- +
* |
* $ Aa ----> ----- +
* |
* V
* + ------------ +
* | Object id2 | -------------- +
* + ------------ + |
* V
* + ------------ ++ ----------------- +
* $ Cc --> | object id2 | ---> | object (Class B) |
* + ------------ ++ ----------------- +
*/

Echo "object aa:"; var_dump ($ aa); // ["testB"] => string (15) "Changed Class B"
Echo "object bb:"; var_dump ($ bb); // ["testB"] => string (15) "Changed Class B"
Echo "object cc:"; var_dump ($ cc); // ["testB"] => string (15) "Changed Class B"

At this time, the content of $ aa, $ bb, and $ cc is exactly the same, so we can see that the object is not transmitted by reference, and we need to get out of this misunderstanding as soon as possible.

Bytes. A reference can be considered as a hardlink in a Unix file system. Reference PHP reference...

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.