Order of execution of recursive function

[php]By my previous understanding, due to the order of execution of recursive functions, when it is called it does not go on to execute the remaining part of the code but creates a new instance of the function in memory, the execution of the rest part of the function occurs at the last instance when the base case is fulfilled. In that light the code snippet

// call display on each of this node’s children
// note a node will only have children in its list if expanded
$num_children = sizeof($this->m_childlist);
for($i = 0; $i<$num_children; $i++)
$row = $this->m_childlist[$i]->display($row, $sublist);
return $row;
will create several instances of the display() function which display the tree, but only return $row at last instance which is in turn returns $row to the instance that calls it until it get to the first. But the writer of the book says the snippet of code sends $row from one call to the next and then the next. How is this possible when the return of $row occurs after the call to the recursive function. What am I getting wrong?

<?php // functions for loading, contructing and // displaying the tree are in this file class treenode { // each node in the tree has member variables containing // all the data for a post except the body of the message var $m_postid; var $m_title; var $m_poster; var $m_posted; var $m_children; var $m_childlist; var $m_depth; function treenode($postid, $title, $poster, $posted, $children, $expand, $depth, $expanded, $sublist) { // the constructor sets up the member variables, but more // importantly recursively creates lower parts of the tree $this->m_postid = $postid; $this->m_title = $title; $this->m_poster = $poster; $this->m_posted = $posted; $this->m_children =$children; $this->m_childlist = array(); $this->m_depth = $depth; // we only care what is below this node if it // has children and is marked to be expanded // sublists are always expanded if(($sublist||$expand) && $children) { $conn = db_connect(); $query = "select * from header where parent = $postid order by posted"; $result = mysql_query($query); for ($count=0; $row = @mysql_fetch_array($result); $count++) { if($sublist||$expanded[ $row['postid'] ] == true) $expand = true; else $expand = false; $this->m_childlist[$count]= new treenode($row['postid'],$row['title'], $row['poster'],$row['posted'], $row['children'], $expand, $depth+1, $expanded, $sublist); } } } function display($row, $sublist = false) { // as this is an object, it is responsible for displaying itself // $row tells us what row of the display we are up to // so we know what color it should be // $sublist tells us whether we are on the main page // or the message page. Message pages should have // $sublist = true. // On a sublist, all messages are expanded and there are // no "+" or "-" symbols. // if this is the empty root node skip displaying if($this->m_depth>-1) { //color alternate rows echo ''; else echo '#ffffff">'; // indent replies to the depth of nesting for($i = 0; $i<$this->m_depth; $i++) { echo ''; } // display + or - or a spacer if ( !$sublist && $this->m_children && sizeof($this->m_childlist)) // we're on the main page, have some children, and they're expanded { // we are expanded - offer button to collapse echo 'Collapse Thread'; } else if(!$sublist && $this->m_children) { // we are collapsed - offer button to expand echo 'Expand Thread'; } else { // we have no children, or are in a sublist, do not give button echo ''; } echo " m_postid >m_postid'>$this->m_title - $this->m_poster - ".reformat_date($this->m_posted).''; echo ''; // increment row counter to alternate colors $row++; } // call display on each of this node's children // note a node will only have children in its list if expanded $num_children = sizeof($this->m_childlist); for($i = 0; $i<$num_children; $i++) { $row = $this->m_childlist[$i]->display($row, $sublist); } return $row; } }; ?>[/php]

Well, I know it works for the few times I have used it. I started looking into how this works and found
so many links to explanation that I gave up.

So, use this query in Google: " php how does recursion work " and you will find tons of info on how
it actually works. One link down a bit even explains how to plan for it’s use. Sorry I did not have an answer.

Thanks Ernie. Truth is, I know how recursive functions work, thats why this one is confusing. I think there’s something I’m missing. Something that explains why the code after each call to the recursive function is executed in each instance and not when control is being returned to the calling function. Any further help will be appreciated. For now i shall remain happily confused.

This is a bit off-topic, but, in searching for further info on your question, I came across Wikipedia’s explanation of recursion. I found it was very interesting. There was a note quite a ways down under item #8 about order of statements and how the results are. Out of time, but, maybe you can get something out of that!


Finally after a 3 hour brain storm, victory!! :D. The article you suggested supported what I already knew about recursive functions which made me understand there had to be something else I was getting wrong.

Assuming we are in a child instance that has a for loop, the loop passes on the value of $row from out of it to the first instance, but if this instance does not have children and thus does not produce a loop, it has to return $row to the instance that called it, and that is still stuck in its for loop with the variable $row waiting to catch this value and set it to the next instance in the loop, or if looping has ended, return this value to the instance that called it… Until the whole tree is created and $row will be returned to the parent loop, which now executes the last line since its own looping has ended, and returns $row to the script that called it, where it is discarded.

Beautiful script.

Thank Ernie…

Sponsor our Newsletter | Privacy Policy | Terms of Service