title: PHP variable visibility in a foreach statement
body: <p>this is my code:</p>
  <pre><code>    $hash = 0;
      foreach ($this-&gt;championship-&gt;getRounds() as $round) {
  
          $ranking = $this-&gt;getRanking($round);
          echo &quot;OLD_HASH: $hash, NEW_HASH: &quot; . $ranking-&gt;getHash();
          if ($hash != $ranking-&gt;getHash()) {
              $ranking-&gt;fullUpdate();
          }
          $hash = $ranking-&gt;getHash();
          echo &quot;SAVE_NEW: $hash&quot;;
  
          $matches = $this-&gt;getMatches($round);
          $matches -&gt;fullUpdate();
      }
  </code></pre>
  <p>If I have 3 objects in the &quot;Rounds&quot; array, and the respective &quot;hash&quot; values are: 1, 2 and 3.</p>
  <p>This is what I expect:</p>
  <blockquote>
  <p>OLD_HASH: 0, NEW_HASH: 1</p>
  <p>SAVE_NEW: 1</p>
  <p>OLD_HASH: 1, NEW_HASH: 2</p>
  <p>SAVE_NEW: 2</p>
  <p>OLD_HASH: 2, NEW_HASH: 3</p>
  <p>SAVE_NEW: 3</p>
  </blockquote>
  <p>Instead this is the real result:</p>
  <blockquote>
  <p>OLD_HASH: 0, NEW_HASH: 1</p>
  <p>SAVE_NEW: 1</p>
  <p>OLD_HASH: 0, NEW_HASH: 2</p>
  <p>SAVE_NEW: 2</p>
  <p>OLD_HASH: 0, NEW_HASH: 3</p>
  <p>SAVE_NEW: 3</p>
  </blockquote>
  <p>WHY?!?</p>