Analyze PHP references through xdebug

Source: Internet
Author: User
Xdebug is used to analyze the reference of PHP which has never understood the reference of php. during the interview today, xdebug seems to understand a little bit and record it .? Code1 :? $ A = "xiaoshenge"; $ B = & amp; $ a; unset ($ B); analyze PHP references through xdebug

I have never understood the reference of php, but I encountered it again during the interview today. I understood it a little with xdebug and recorded it.

?

Code1:

?

$a = "xiaoshenge";$b = &$a;unset($b);echo "b=$b";echo "a=$a";

Result: B = a = xiaoshenge

?

Code2:

$a = "xiaoshenge";$b = &$a;unset($a);echo "b=$b";echo "a=$a";

Result: B = xiaoshengea = ??

?

Guess:

?

It was code1 during the interview. because I didn't understand the reference of PHP, I guessed that I wrote B = a =. In fact, this is related to the mixing of references with pointers in c. After I came back, I debugged code2 and was totally confused. so I asked for help and the PHP documentation.

?

Reference in the PHP document:

?

?

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.

?

Unreference: When unset is a reference, it only disconnects the binding between the variable name and the variable content. This does not mean that the variable content is destroyed. For example:

?

 

? Not unset?$ B, Just?$. And Unix?Unlink? The call may be helpful for understanding.

?

Inference:

?

The document introduces "reference can be considered as a hardlink in unix File Systems", refer to the http://bbs.chinaunix.net/forum.php? Mod = viewthread & tid = 150986? In:

Hard connectionIs to provide a copy of the file, and establish a connection between the two. Modify one of them, and the connected file is modified at the same time. If you delete any [/color] of the [color = red] files, the remaining files will not be affected .?

Soft connectionIt is also called a symbolic connection. it just creates a "quick (borrow wondows frequently used words)" for the source file at a new location. Therefore, when the source file is deleted, the file connected by the symbol will become a source-> there is only one file name left. of course, deleting this connection will not affect the source file, but the use and reference of the connection file will directly call the source file .?

?

Xdebug shows the changes in the zval container:

?

Code1:

?

$a = "xiaoshenge";$b = &$a;xdebug_debug_zval( 'a' );xdebug_debug_zval( 'b' );unset($b);xdebug_debug_zval( 'a' );xdebug_debug_zval( 'b' );

Result:

?

A:

(refcount=2, is_ref=1),string 'xiaoshenge' (length=10)

B:

(refcount=2, is_ref=1),string 'xiaoshenge' (length=10)

A:

(refcount=1, is_ref=0),string 'xiaoshenge' (length=10)

?

Code2:

?

$a = "xiaoshenge";$b = &$a;xdebug_debug_zval( 'a' );xdebug_debug_zval( 'b' );unset($a);xdebug_debug_zval( 'a' );xdebug_debug_zval( 'b' );

? Result:

A:

(refcount=2, is_ref=1),string 'xiaoshenge' (length=10)

B:

(refcount=2, is_ref=1),string 'xiaoshenge' (length=10)

B:

(refcount=1, is_ref=0),string 'xiaoshenge' (length=10)

?

?

In combination with the hard link of unix (it should be as follows ):


Unset ($ a), only destroys part a-x, so $ B is still there.

?

$ B = & $ B, not like a pointer. $ B points to $ a. (pointer, it seems that my C has been returned to the book and I want to study it)

?

Interview experience: it is difficult for people to get started and think for themselves. The PHP Manual cannot be ignored. This document contains the following content.

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.