A side question for a variable reference in the "PHP" Foreach Loop

Source: Internet
Author: User
Tags php source code

$a = Array (' A ', ' B ', ' C '), foreach ($a as & $v) {}foreach ($a as $v) {}var_dump ($a);

Right now. Don't open your browser and guess. What is the result of the output?

Children's shoes, which are more familiar with references, may already be seen.  The correct answer is: Array (3) {[0]=> string (1) "A" [1]=> string (1) "B" [2]=> &string (1) "B"} is a,b,b. If you're guessing that's a,b,c. So for reference, you need to look at the relevant information: http://www.php.net/manual/zh/language.references.php

So why is it a,b,b? Let's take a step-by-step look at:

We know that when a foreach loop is executed on an array, it is implemented by moving the internal pointers to the arrays (for more details, you can read the PHP source code). So for the example in this article: when the Foreach loop ends, because $v is

Reference variables, so $v and $a [2] point to the same address space (shared variable values), and any subsequent modifications to $v will be reflected directly in the array $ A. We can add debug code to the example, we will be clear, for example, we in the second loop inside, plus var_dump ($a), testing each cycle when the value of a change:

$a = Array (' A ', ' B ', ' C '), foreach ($a as & $v) {}foreach ($a as $v) {var_dump ($a); echo "<br/>";} Var_dump ($a);

Run the code. The result is:

Draw a picture: you can see it more clearly: (the "$v pointing to $a[2]" is not accurate. It should be: $v and $a[2] point to the same place)

A few simple explanations for references:

1. A reference is similar to a pointer, but differs from a pointer.

For example, for references:

$a = "str"; $b = & $a;//<var class= "varname" ><var class= "varname" > $a </var></var> and <var class= "varname" ><var class= "varname" > $b </var></var> points to the same place

A simple one is as follows:

Then change the value of any element in $ A and $b at this time. The other value is changed as follows:

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

2.unset will only delete the variable. Does not empty the memory space corresponding to the variable value: (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 internally by the function:

function change (& $a) {if (Is_array ($a)) {$a = array ();}} $test = Range (1,10), change ($test);p rint_r ($test);

Based on the above points, you should use the reference carefully during the encoding process. prevented from falling into inexplicable embarrassment.

PS: Do you understand? Try this question:

$a = range (1,3), foreach ($a as & $b) {    $b *= $b;} foreach ($a as $b) {    echo  $b;}   

Guess what the output is?

A side question for a variable reference in the "PHP" Foreach Loop

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.