<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Cloudspace &#124; Blog &#187; php</title>
	<atom:link href="http://www.cloudspace.com/blog/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.cloudspace.com/blog</link>
	<description>All things cloudspacious</description>
	<lastBuildDate>Mon, 16 Jan 2012 22:58:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Recursion and PHP</title>
		<link>http://www.cloudspace.com/blog/2007/08/02/recursion-and-php/</link>
		<comments>http://www.cloudspace.com/blog/2007/08/02/recursion-and-php/#comments</comments>
		<pubDate>Fri, 03 Aug 2007 03:47:17 +0000</pubDate>
		<dc:creator>Tim Rosenblatt</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[tech]]></category>

		<guid isPermaLink="false">http://www.cloudspace.com/blog/?p=9</guid>
		<description><![CDATA[I&#8217;m working on a side project, naturally in PHP (v4.4.1 to be specific). There&#8217;s a lot of string parsing, and regex just wasn&#8217;t able to handle it. When it worked, it would take 30+ seconds; often it timed out. I &#8230; <a href="http://www.cloudspace.com/blog/2007/08/02/recursion-and-php/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m working on a side project, naturally in PHP (v4.4.1 to be specific). There&#8217;s a lot of string parsing, and <a href="http://en.wikipedia.org/wiki/Regular_expression" title="Regular Expressions on Wikipedia">regex</a> just wasn&#8217;t able to handle it. When it worked, it would take 30+ seconds; often it timed out. I went and wrote some tail-recursive functions that would do the parsing, but much faster (it works about 90% of the time and load times take 2-3 seconds).</p>
<p>Unfortunately, PHP doesn&#8217;t work well with recursion. For one, there&#8217;s no real good way to release memory, so it&#8217;s easy to use up all the available memory quickly. Oh well, so be it. That&#8217;s how the language is built.</p>
<p>But there&#8217;s another problem. It seems that after a few thousand recursive calls, PHP gives up the ghost. To be fair, a few thousand recursive calls isn&#8217;t a terribly small number, but what it does to PHP is strange.</p>
<p>For example:</p>
<p>&lt;?php<br />
error_reporting(E_ALL); /* Lets be as fair as possible, if there&#8217;s an error, we want to know */</p>
<p>function a($v) {<br />
if($v&gt;10000) { exit(); } /* This recursive function *will* stop */<br />
echo $v . &#8216;&lt;br /&gt;&#8217;;<br />
a($v+1);<br />
}</p>
<p>a(0);<br />
?&gt;</p>
<p>Running this code (as I said, I&#8217;m using PHP4.4.1 here) produces:</p>
<p>0<br />
1<br />
2<br />
3<br />
&#8230;<br />
3769<br />
3770<br />
3771<br />
37</p>
<p>That&#8217;s it. It doesn&#8217;t even echo 3772 entirely, so it doesn&#8217;t seem that the call stack is just reaching some maximum value and not letting the function call go through.  It just dies. It always stops at &#8217;37&#8242;, right after &#8217;3771&#8242;.</p>
<p>Weird.</p>
<p>In case you&#8217;re wondering, I&#8217;m going to rewrite the recursion to use a while loop. That&#8217;ll solve the speed issues, and the memory issues, and the magic recursion-stack-call issues.</p>
<p>Edit: The while loop did work, and it seems just as fast. It&#8217;s not as appealing to the Computer Scientist in me, but it does work correctly, and ultimately, that&#8217;s what matters.</p>
<p>As an example of turning recursion into a while loop (converting recursion to iteration), here&#8217;s the above code, rewritten.</p>
<p>&lt;?php<br />
error_reporting(E_ALL); /* Lets be as fair as possible, if there&#8217;s an error, we want to know */</p>
<p>$v = 0;</p>
<p>while($v&lt;10000) {<br />
echo $v . &#8216;&lt;br /&gt;&#8217;;<br />
a($v+1);<br />
}<br />
?&gt;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.cloudspace.com/blog/2007/08/02/recursion-and-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

