title: Optimizing a Recursive Method In PHP
body: <p>I'm writing a text tag parser and I'm currently using this recursive method to create tags of <em>n</em> words. Is there a way that it can be done non-recursively or at least be optimized? Assume that $this->dataArray could be a very large array.</p>
  
  <pre><code>/**
   * A recursive function to add phrases to the tagTracker array
   * @param string $data
   * @param int $currentIndex
   * @param int $depth
   */ 
  protected function compilePhrase($data, $currentIndex, $depth){
      if (!empty($data)){
          if ($depth &gt;= $this-&gt;phraseStart){
              $this-&gt;addDataCount($data, $depth);
          }
          if ($depth &lt; $this-&gt;phraseDepth){
              $currentIndex = $currentIndex + 1;
              //$this-&gt;dataArray is an array containing all words in the text
              $data .= ' '.$this-&gt;dataArray[$currentIndex]; 
              $depth += 1;
              $this-&gt;compilePhrase($data, $currentIndex, $depth);
          }
      }
  }
  </code></pre>