title: Handle foreach by-ref in PHP 7
body: <p>So far we've been using PHP 5.5 and everything seemed to be flowing too good with the code. Since upgrading it to 7 most of the foreach() seems to be having inconsistent behaviour. </p>
  
  <p>For eg: Consider the snippet below:</p>
  
  <pre><code>$array = array('a', 'b', 'c');
  self::testForeach($array);
  .
  .
  .
  // $array is passed by reference
  public static function testForeach(&amp;$array) {
  
    foreach ($array as $key =&gt; $val) {
      //produces a, b as an output in PHP 5
      //produces a, b, c as an output in PHP 7
      var_dump($val);
  
      if ($val == 'b') {
        //remove 'c' from the array
        unset($array[2]);
      }
    }
  }
  </code></pre>
  
  <p>Behaviour in PHP 5.5:</p>
  
  <blockquote>
    <p>$array is passed by reference to testForeach() function. So removing "c" from $array inside the loop would directly modify the original array. Hence, the iterated values would be a, b and not c as it gets removed from the array in between.</p>
  </blockquote>
  
  <p>Behaviour in PHP 7:</p>
  
  <blockquote>
    <p>$array is passed by reference to testForeach() function. When $array is looped over foreach(), a copy is made say $arrayCopy (according to the doc) which is being iterated over the loop. So removing "c" value from $array would have no effect and will loop all the values contained in the $arrayCopy. 
    Hence the output - a, b, c.</p>
  </blockquote>
  
  <p>Changing foreach to pass-by-ref is not a soln for me as there are too many foreach on my project and I can't grep and modify each of them.</p>
  
  <p>Is there any other handling done for such behaviour on the latest version. Any tool/parser which could highlight them?</p>
  
  <p>Any hints/ideas ?</p>
  
  <p>Thanks!</p>