title: PHP, an odd variable scope?
body: <p>This more a question about the why then 'how-to', yet it has been annoying me for some days now. Currently I am doing some work with CodeIgniter and going back to PHP temporarily from Ruby, bugs me about the following scoping magic.</p>
  
  <pre><code>&lt;?php $query = $this-&gt;db-&gt;get('articles', 2);
          if ($query-&gt;num_rows() &gt; 0)
          {
             foreach ($query-&gt;result_array() as $row)
             {
                $data[] = $row; # &lt;-- first appearance here
             }
          return $data; # &lt;--- :S what?!
          } 
  </code></pre>
  
  <p>As you can see, I am not exactly a PHP guru, yet the idea of local scope bugs me that outside the foreach loop the variable is 'available'. So I tried this out inside a view:</p>
  
  <pre><code>&lt;?php
  
      if($a==1)
      {
      $b = 2;
      }
      echo $b;
  ?&gt;
  </code></pre>
  
  <p>Which result in an error message:</p>
  
  <pre><code>Message: Undefined variable: b
  </code></pre>
  
  <p>The PHP manual tells about the local scoping, yet I am still wondering why this happens and if there are special rules I do not know about. And it scares me :)</p>
  
  <p>Thanks for sharing ideas,</p>