Interview question-why is this the problem of passing PHP references?

Source: Internet
Author: User
1) Why is the result of $ a hello instead of undefined? {Code...} 2) Why is the result of $ a 1 instead of 2? {Code...} 1) Why is the $ a result hello instead of undefined?

$a = "hello";$b =& $a;unset($b);$b = "world";var_dump($a); // hello

2) Why is the result of $ a 1 instead of 2?

$a = 1;$x =& $a;$b = $a++;var_dump($b); // 1

Reply content:

1) Why is the result of $ a hello instead of undefined?

$a = "hello";$b =& $a;unset($b);$b = "world";var_dump($a); // hello

2) Why is the result of $ a 1 instead of 2?

$a = 1;$x =& $a;$b = $a++;var_dump($b); // 1

1. refer to the definition of unset in the php document.

If unset () is a variable passed by reference in the function, only the local variable is destroyed, and the variables in the call environment will keep the same value before the call of unset. Php unset

Therefore, unset $ B has no effect on $.

2. refer to the definition of the increment/decrease operator in the php document.

$ A ++ returns $ a after adding, and then adds the value of $. Php increment/decrease Operator

$ A ++ first returns 1 value to $ B, so $ B is 1

1. B is a shortcut, vest. Deleted shortcuts are not affected.
2, $ a ++ indicates that the statement is executed only after + 1. Change the result of expected 2 to $ B = ++ $ a; or var_dump ($ );.

  • $bYes$aAre two different variables pointing to the same physical address. When executedunset($b)Only logout$bThis variable is used instead of deregistering the value in the physical address.$aIt is still the original valuehello.

  • You need to understand this question.$a++And++$a.

// $ A ++ is equivalent to $ B = $ a; $ a + = 1; // ++ $ a is equivalent to $ a + = 1; $ B = $;

Therefore,$a++Is to assign a value first and then auto-increment, so$bThe value is 1.

The unset () function only removes $ B's reference to $.
Therefore, the space that $ a points to is retained, but the reference count and is_ref mark of $ B are deleted.
Therefore, $ a does not comply with the garbage collection mechanism.
The reference mechanism of PHP can obtain answers from the basic knowledge of reference count.

The first reason is that unset () is a variable passed by reference, but the local variable is destroyed, and the variables in the call environment will keep the same value before the call of unset.

The second problem is the computing priority. ++ calculates the expression after the First Auto-increment, ++ calculates the expression before the first and then the auto-increment.

First, let's talk about the understanding of variables in PHP: variables can be seen as a box, while variable names are equivalent to the labels in this box. Application transfer is equivalent to adding two labels to the same box, the value transfer re-creates an identical box!
The local variable name is stored in the stack memory. The unset () function only disconnects the pointing address of a variable from the actual storage location of the variable. This removes the box tag! Therefore, unset ($ B) Only disconnects $ B from the actual storage location, so the space of $ B in the stack memory will be recycled, but there is still the $ a label on this box, so it will not be recycled by the garbage collection mechanism! Therefore, you can still find the output $ a (correct addressing), but now you cannot find the output $ B!
As for why the second one is not 2, other people's answers are clear! This is the difference between the prefix operator ++ $ a and the Postfix operator $ a ++!
+ $ A: perform all operations on $ a after auto-increment.
$ A ++: auto-increment after $ a is operated

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.