Originally Posted by dniwebdesign
Alrighty, I have the above function, which right now should count to 11 then return the number (which should be 11). However although it does do the recursive and counts all the way to 11 it does not return the value once it is greater than 10. Why?
If you want to use the return value of a recursive function it has to return itself instead of just calling itself.
The way you've written it, the first call, with a depth <= 10, calls another recursion of getDepth but itself runs to completion without hitting a return. If you change that line to return $this->getDepth(0, $depth), though, it will create a chain of returns ending at the terminating condition.
return ($depth > 10) ? $depth : getDepth_2(++$depth);
echo getDepth_2(0); // 11