[PHP] An interview question referenced by variables in the foreach Loop

Source: Internet
Author: User

A friend met an interview question when I went to Kingsoft yesterday to interview php development. It was about reference and foreach loop. A basic question

Let's talk about the Code:

$a = array('a','b','c');foreach($a as &$v){}foreach($a as $v){}var_dump($a);

Now. Don't open your browser. just guess. What is the output result?

The reference may be well understood. The correct answer is: array (3) {[0] => string (1) "A" [1] => string (1) "B" [2] => & string (1) "B"} That is, A, B, B. if you guess a, B, c. So on reference, you also need to check the relevant information: http://www.php.net/manual/zh/language.references.php

So why is it a, B, and B. Let's look at it step by step:

We know that when we execute a foreach loop on the array, it is implemented by moving the internal pointer of the array (for more details, refer to the PHP source code ). So for the example in this article: When the foreach loop ends, because $ V is

Therefore, $ V and $ A [2] point to the same address space (shared variable value ), therefore, any subsequent changes to $ V will be directly reflected in the array $. We can add the debugging code to the example and it will be clear. For example, we add var_dump ($ A) inside the second loop to test the value change of A in each loop:

$a = array('a','b','c');foreach($a as &$v){}foreach($a as $v){var_dump($a);echo "<br/>";}var_dump($a);

Run the code. Result:

array(3) { [0]=> string(1) "a" [1]=> string(1) "b" [2]=> &string(1) "a" }array(3) { [0]=> string(1) "a" [1]=> string(1) "b" [2]=> &string(1) "b" }array(3) { [0]=> string(1) "a" [1]=> string(1) "b" [2]=> &string(1) "b" }array(3) { [0]=> string(1) "a" [1]=> string(1) "b" [2]=> &string(1) "b" } 

Draw a picture: It can be clearer: (the figure "$ V points to $ A [2]" is not accurate. It should be: $ V and $ A [2] point to the same place)

Some simple explanations about the reference:

1. References are similar to pointers, but different from pointers.

For example, for reference:

$ A = "str"; $ B = & $ ;//$And$ BPoint to the same place

A simple example is as follows:

Change the values of any element in $ A and $ B. The other values change accordingly:

$a = "str";$b = &$a;$b = "sssss";echo $a;

2. unset only deletes the variable. The memory space corresponding to the variable value is not cleared: (this is different from the pointer)

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

3. When a reference is passed as a function parameter, it can be changed inside the function:

function change(&$a){if(is_array($a)){$a = array();}}$test = range(1,10);change($test);print_r($test);

Based on the above points, be careful to use the reference during the encoding process. Prevent inexplicable embarrassment.

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.