Home > Backend Development > PHP Tutorial > Problems caused by using references in foreach

Problems caused by using references in foreach

WBOY
Release: 2016-07-29 09:12:01
Original
887 people have browsed it

No more nonsense, just look at the following code:

<code><span>$arr</span> = <span>array</span>(<span>'a'</span>,<span>'b'</span>,<span>'c'</span>,<span>'d'</span>);
     <span>foreach</span>(<span>$arr</span><span>as</span> &<span>$v</span>){}

     <span>foreach</span>(<span>$arr</span><span>as</span><span>$v</span>){
          var_dump(<span>$v</span>);
     }</code>
Copy after login

After running, the results are as follows:

<code><span>string</span><span>'a'</span> (<span>length</span>=<span>1</span>)
<span>string</span><span>'b'</span> (<span>length</span>=<span>1</span>)
<span>string</span><span>'c'</span> (<span>length</span>=<span>1</span>)
<span>string</span><span>'c'</span> (<span>length</span>=<span>1</span>)    <span> //这里为什么是c呢?</span></code>
Copy after login

We will be surprised, why is the last one c instead of d?

In fact, the reason is very simple. After the first loop ends, the variable v becomes a reference to arr[3]. Therefore, when foreach is used for the second time, the value-passing method is used. This method actually traverses the value of variable arr, and then assigns the value to variable v, because variable v is a reference to arr[3] at this time. , so assigning the value to variable v is actually assigning it to arr[3].

Let’s change the code:

<code><span>$arr</span> = <span>array</span>(<span>'a'</span>,<span>'b'</span>,<span>'c'</span>,<span>'d'</span>);
     <span>foreach</span>(<span>$arr</span><span>as</span> &<span>$v</span>){}

     <span>foreach</span>(<span>$arr</span><span>as</span><span>$v</span>){
          var_dump(<span>$arr</span>);<span>//这里把变量$arr打印出来</span>
     }</code>
Copy after login

After running it, we will find:

<code>array (size=<span>4</span>)
  <span>0</span> => <span>string</span><span>'a'</span> (<span>length</span>=<span>1</span>)
  <span>1</span> => <span>string</span><span>'b'</span> (<span>length</span>=<span>1</span>)
  <span>2</span> => <span>string</span><span>'c'</span> (<span>length</span>=<span>1</span>)
  <span>3</span> => &<span>string</span><span>'a'</span> (<span>length</span>=<span>1</span>)
array (size=<span>4</span>)
  <span>0</span> => <span>string</span><span>'a'</span> (<span>length</span>=<span>1</span>)
  <span>1</span> => <span>string</span><span>'b'</span> (<span>length</span>=<span>1</span>)
  <span>2</span> => <span>string</span><span>'c'</span> (<span>length</span>=<span>1</span>)
  <span>3</span> => &<span>string</span><span>'b'</span> (<span>length</span>=<span>1</span>)
array (size=<span>4</span>)
  <span>0</span> => <span>string</span><span>'a'</span> (<span>length</span>=<span>1</span>)
  <span>1</span> => <span>string</span><span>'b'</span> (<span>length</span>=<span>1</span>)
  <span>2</span> => <span>string</span><span>'c'</span> (<span>length</span>=<span>1</span>)
  <span>3</span> => &<span>string</span><span>'c'</span> (<span>length</span>=<span>1</span>)
array (size=<span>4</span>)
  <span>0</span> => <span>string</span><span>'a'</span> (<span>length</span>=<span>1</span>)
  <span>1</span> => <span>string</span><span>'b'</span> (<span>length</span>=<span>1</span>)
  <span>2</span> => <span>string</span><span>'c'</span> (<span>length</span>=<span>1</span>)
  <span>3</span> => &<span>string</span><span>'c'</span> (<span>length</span>=<span>1</span>)</code>
Copy after login

It can be found that the value of arr[3] keeps changing.

').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

Copyright Statement: This article is an original article by the blogger. Please indicate the source when reprinting.

The above introduces the problems caused by using references in foreach, 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