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

Problems caused by using references in foreach

WBOY
WBOYOriginal
2016-07-29 09:12:01904browse

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>

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>

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>

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>

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.

Statement:
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