In Recurive funaction..(code using PHP and MySql)
When using a recursive function try not to use any iterative loops.
It increases the number of iterations at execution time.
In this example I am trying make a URL using a database and find the actual URL.
Both results are the same but the number of iterations is three times in the 2nd
option.
This is was practically happens :
<? //correct One function get_scat_url($temp_catid) { static $url; global $site_id=5; if ($temp_catid != 0) { $temp_sql=db_query("select sm_cat_id, sm_cat_pcat_id, sm_cat_url, sm_cat_parentid from sitemap_category where sm_cat_id ='". $temp_catid . "' and sm_domain_id ='". $site_id . "'"); if (db_num_rows($temp_sql) > 0) { $rec_set = db_fetch_array($temp_sql); $url = $rec_set['sm_cat_url']. " : " . $url; $temp_catid = $rec_set['sm_cat_parentid']; get_scat_url($temp_catid); } } //first if statement return $url; } ?> <? //incorrect recursive call in php program function get_scat_url($temp_catid){ //global $url; $site_id=5; while($temp_catid != 0) { $temp_sql=db_query("select sm_cat_id, sm_cat_pcat_id, sm_cat_url, sm_cat_parentid from sitemap_category where sm_cat_id ='". $temp_catid . "' and sm_domain_id ='". $site_id . "'"); if (db_num_rows($temp_sql) > 0){ $rec_set = db_fetch_array($temp_sql); $url = $rec_set['sm_cat_url']. " : " . $url; $temp_catid = $rec_set['sm_cat_parentid']; get_scat_url($temp_catid); } } //while ends here... return $url; } ?>