title: Why am I getting in an infinite loop with this parent-child recursion?
body: <p>I have the following two functions which should return a treeview-like dropdown.</p>
  
  <p>PHP:</p>
  
  <pre><code>function getCategories()
  {
      $src = $this-&gt;db-&gt;query('SELECT * FROM categories')-&gt;fetchAll();
      return $this-&gt;tree($src, 0, '');
  }
  
  function tree($src, $level, $html)
  {
      $dashes = '';
      for($i=0;$i&lt;$level;$i++)
      {
          $dashes .= '-';
      }
  
      foreach($src as $row)
      {
          if($row['parent'] == $level)
          {
              $html .= '&lt;option value="'. $row['id'] .'"&gt;'. $dashes . $row['name'] .'&lt;/option&gt;';
          }
      }
  
      $html .= $this-&gt;tree($src, $level + 1, $html);
  
      return $html;
  }
  </code></pre>
  
  <p>Expected HTML result:</p>
  
  <pre><code>&lt;option value="1"&gt;Category1&lt;/option&gt;
  &lt;option value="2"&gt;-Category2&lt;/option&gt;
  &lt;option value="3"&gt;--Category3&lt;/option&gt;
  &lt;option value="4"&gt;-Category4&lt;/option&gt;
  </code></pre>
  
  <p>But it leads to an infinite loop instead. Why?</p>