title: Convert function from recursion to iteration
body: <p>I have this function that I wrote that is abysmally slow since php does not handle recursion well.  I am trying to convert it to a while loop, but am having trouble wrapping my head around how to do it.</p>
  
  <p>Can anyone give me some tips?</p>
  
  <pre><code>    public function findRoute($curLoc, $distanceSoFar, $expectedValue) {
  
      $this-&gt;locationsVisited[$curLoc] = true;
      $expectedValue += $this-&gt;locationsArray[$curLoc]*$distanceSoFar;
  
      $at_end = true;
      for($i = 1; $i &lt; $this-&gt;numLocations; $i++) {
          if($this-&gt;locationsVisited[$i] == false) {
              $at_end = false;
  
              if($expectedValue &lt; $this-&gt;bestEV)
                  $this-&gt;findRoute($i, $distanceSoFar + $this-&gt;distanceArray[$curLoc][$i], $expectedValue);
          }
      }
  
      $this-&gt;locationsVisited[$curLoc] = false;
  
      if($at_end) {
          if($expectedValue &lt; $this-&gt;bestEV) {
              $this-&gt;bestEV = $expectedValue;
          }
      }
  }
  </code></pre>