Home > Backend Development > PHP Tutorial > A 'pit' in PHP

A 'pit' in PHP

WBOY
Release: 2016-08-08 09:27:12
Original
1223 people have browsed it

 Talk about a problem that you are likely to encounter at work - a quote from foreach

<span>$arr</span> = <span>range</span>(1,3<span>);
</span><span>//</span><span>[1,2,3]</span>

<span>foreach</span>(<span>$arr</span> <span>as</span> &<span>$val</span><span>) {
}

</span><span>foreach</span>(<span>$arr</span> <span>as</span> <span>$val</span><span>) {
}
</span><span>print_r</span>(<span>$arr</span><span>);
 </span>
Copy after login

 What does the above code output? The amazing thing is that it is as follows. I encountered this once at work. It took me a long time to figure out the reason, but I just found a solution. There are two solutions to this problem:

<span>Array</span><span>
(
    [</span>0] => 1<span>
    [</span>1] => 2<span>
    [</span>2] => 2<span>
)</span>
Copy after login

The following two methods can solve the above problem:

<span>//</span><span>方法1</span>
<span>foreach</span> (<span>$arr</span> <span>as</span> &<span>$value</span><span>) {
}
</span><span>unset</span>(<span>$value</span><span>);
</span><span>foreach</span> (<span>$arr</span> <span>as</span> <span>$value</span><span>) {
}
</span><span>print_r</span>(<span>$arr</span><span>);
</span><span>//</span><span>[1,2,3]

//方法2</span>
<span>foreach</span> (<span>$arr</span> <span>as</span> &<span>$value</span><span>) {
}
</span><span>foreach</span> (<span>$arr</span> <span>as</span> <span>$val</span><span>) {
}
</span><span>print_r</span>(<span>$arr</span><span>);
</span><span>//</span><span>[1,2,3]
//方法3</span>
<span>foreach</span> (<span>$arr</span> <span>as</span> &<span>$value</span><span>) {
}
</span><span>foreach</span> (<span>$arr</span> <span>as</span> &<span>$value</span><span>) {
}
</span><span>print_r</span>(<span>$arr</span><span>);
</span><span>//</span><span>[1,2,3]</span>
Copy after login

Method 1 can also be seen in the official manual http://php.net/manual/en/control-structures.foreach.php. There is a special tip in the article to remind you of this

Why are Method 2 and Method 3 OK? You will know after reading the following. In fact, it is the reference in "causing trouble". The &$value in foreach is a pointer variable pointing to an element in the array. Let's look at the following first. This It’s easy to understand. tmp is a reference to var, pointing to the storage space of var. When tmp changes, var also changes

<span>$var</span> = 123<span>;
</span><span>$tmp</span> = &<span>$var</span><span>;
</span><span>$tmp</span> = 200<span>;
</span><span>echo</span> <span>$var</span><span>;
</span><span>//</span><span>200</span>
Copy after login

Take a look at the stolen picture below (haha, the original link is posted below) to have a better understanding of the above

Okay, after this is ok, look at the following (I don’t know what software was used to draw the above picture, I can only draw it by hand, embarrassing~~), let’s look at the first foreach first:

Second foreach:

At this time, it is easy to know that in fact, in the second foreach loop, the value of the last element in the array is really changeable~~, from the first element to the second to last element. To solve this problem, it is very simple, cut off $ The relationship between value and the second foreach. Therefore, you can unset, change the variable name, or reset the pointer (method 3). Well, at this point, you can basically understand the problem at this point.

Okay, let’s take a look at the comments below the official document. It should be easy to understand

<?<span>php
</span><span>$arr1</span> = <span>array</span>("a" => 1, "b" => 2, "c" => 3<span>);
</span><span>$arr2</span> = <span>array</span>("x" => 4, "y" => 5, "z" => 6<span>);

</span><span>foreach</span> (<span>$arr1</span> <span>as</span> <span>$key</span> => &<span>$val</span><span>) {}
</span><span>foreach</span> (<span>$arr2</span> <span>as</span> <span>$key</span> => <span>$val</span><span>) {}

</span><span>var_dump</span>(<span>$arr1</span><span>);
</span><span>var_dump</span>(<span>$arr2</span><span>);
</span>?><span>

The output is</span>:
<span>array</span>(3) { ["a"]=> int(1) ["b"]=> int(2) ["c"]=> &int(6<span>) }
</span><span>array</span>(3) { ["x"]=> int(4) ["y"]=> int(5) ["z"]=> int(6) }
Copy after login

Reference article:

 http://www.cnblogs.com/CraryPrimitiveMan/p/4030748.html#3085766

http://www.jb51.net/article/39299.htm

 The copyright of this article belongs to the author iforever (luluyrt@163.com). Any form of reprinting is prohibited without the consent of the author. After reprinting the article, the author and the original text link must be given in an obvious position on the article page, otherwise legal liability will be reserved. right.

The above introduces a "pit" in PHP, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template