PHP reference variable, php reference variable

Source: Internet
Author: User

PHP reference variable, php reference variable
1. Definition of referenced Variables

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

Using it will direct (for example, $ a = & $ B) to the same memory address (this is not like a C pointer: for example, you cannot perform pointer operations on them, they are not actually memory addresses). One changes, and the other changes.

2. Use the memory_get_usage () function to observe Memory changes by 2.1. Do not use &

In PHP, COW (Copy On Write) causes the assignment to reference the address of the previous variable (the memory will not change much). Only when a Write operation occurs, to open up a new memory address.

$a = range(0,1000);var_dump(memory_get_usage());$b = $a;var_dump(memory_get_usage());$a = range(0,1000);var_dump(memory_get_usage());
Running result:

There is no big difference between the first time and the second time, and the third time has a big difference.

2.2 Usage &
$a = range(0,1000);var_dump(memory_get_usage());$b = &$a;var_dump(memory_get_usage());$a = range(0,1000);var_dump(memory_get_usage());

Running result:

There is basically no change in the process. Although the write operation is performed on the third step, the same address referenced by $ a and $ B does not need to be opened.

3. Use xdebug to observe

There are many ways to install xdebug on the Internet. I will not elaborate on it here (a php extension plug-in)

3.1 don't use &
// Zval variable container $ a = range (0, 3); xdebug_debug_zval ('A'); // defines variable B, assign the value of a to B $ B = $ a; xdebug_debug_zval ('A'); // modify a $ a = range (0, 3 ); xdebug_debug_zval ('A ');

Run the following command:

Refcount is used to identify the number of variables pointing to the zval variable container

Is_ref (bool) to identify whether the variable belongs to the reference set

Step 2 only performs the COPY operation, so that $ a, $ B points to the same memory address, refcount = 2, and step 3 has a write operation (is_ref = 0 is not a reference ), re-opened the memory address, refcount = 1

3.2 Usage &
// Zval variable container $ a = range (0, 3); xdebug_debug_zval ('A'); // defines variable B, assign the value of a to B $ B = & $ a; xdebug_debug_zval ('A'); // modify a $ a = range (0, 3 ); xdebug_debug_zval ('A ');

Running result:

The reference (&) is used. Therefore, starting from step 2, refcount = 2, is_ref = 1 (reference), no new memory address is opened in the reference state;

4. Special Reference (object)

In php, the OBJECT itself is the reference to pass the value (since PHP 5, new automatically returns the reference, so here we use= &Messages of the E_STRICT level are generated when they are out of date .)

// The object itself refers to passing the class Person {public $ name = 'zs';} $ p1 = new Person; xdebug_debug_zval ('p1'); $ p2 = $ p1; xdebug_debug_zval ('p1'); $ p2-> name = 'LS'; xdebug_debug_zval ('p1 ');

Running result:

When an OBJECT is assigned a value, the memory address is shared, but it is not referenced.

5. unset
// Unset only cancels the reference and does not destroy the space $ a = ''; xdebug_debug_zval ('A'); $ B = & $ a; xdebug_debug_zval ('A '); unset ($ B); xdebug_debug_zval ('A ');

Running result:

So in the first step, refcount = 0

Corresponding reference (&), unset will only cancel the reference, but will not destroy the memory address

5. Summary

Is_ref is used to determine whether the variable is referenced. If the variable is referenced, it is directly modified (the original memory address) when it is modified. Otherwise, it needs to be separated (a new address is opened ), the usset variable only cancels the reference of the variable, but does not eliminate the memory address. The memory can be recycled only when refcount = 0.

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.