Recursively get the Role ID string function Exploderole ($ROLEOBJ, & $resultStr) {if (0 < count ($roleObj->childroleobjarr)) { foreach ($roleObj->childroleobjarr as $CHILDROLEOBJ) {if (' = = $resultStr) {$resultStr. = "{$CHILDROLEOBJ-&G T;id} "; }else{$resultStr. = ", {$childRoleObj->id}"; } exploderole ($CHILDROLEOBJ, $RESULTSTR); }}//recursively gets the cascading role information array function makerolerelation (& $ROLEOBJARR) {foreach ($roleObjArr as $item) {$item->childroleobj ARR = Getroleobjarrbyparentid ($item->id); if (0 < count ($item->childroleobjarr)) {makerolerelation ($item->childroleobjarr); }}//Gets the child role information through the ID of the parent role function Getroleobjarrbyparentid ($parentid) {$operCOGPSTRTSysRole = new cogpstrtsysrole (); $operCOGPSTRTSysRole->setcolumn ($operCOGPSTRTSysRole->getallcolumn ()); $operCOGPSTRTSysRole->setwhere ("parentroleid={$parentid}"); $ROLEOBJARR = $operCOGPSTRTSysRole->convresult2objarr ($operCOGPSTRTSysRole->selecttable ()); return Isset ($rOleobjarr)? $ROLEOBJARR: Array ();}
Recursive function Usage for PHP
A function calls itself in its function body called a recursive call. This function is called a recursive function. This is usually a high practical value for programmers, often used to break down complex problems into simple and identical situations, and do this repeatedly until the problem is solved.
The difference between recursive function and no recursive function
Example one: using static variables
function test () { static $dig =0; if ($dig ++<10) { echo $dig; Test ();} } Test ();//12345678910
Example two: Using recursive functions and loops to implement string reversal permutations
function Unreverse ($str) { for ($i =1; $i <=strlen ($STR); $i + +) { echo substr ($str,-$i, 1); }} Unreverse ("ABCDEFG");//gfedcbcfunction reverse ($str) { if (strlen ($STR) >0) { reverse (substr ($STR, 1)); Echo substr ($STR, 0,1); return;} } Reverse ("ABCDEFG");//GFEDCBC
Recursive functions many times we can recycle, suggesting that we don't use loops instead, because we're easier to understand with loops, and less prone to mistakes.
PHP Recursive functions PHP pay recursive functions, recursive functions are called themselves, these functions are particularly useful for browsing dynamic data structures, such as trees and lists.
Few Web applications require complex data structures to be used
<?phpfunction Reversr_r ($STR) {if (strlen ($STR) >0) Reverse_r (substr ($STR, 1)); Echo substr ($STR, 0,1); return;}? ><?phpfunction reverse_i ($STR) {for ($i =1; $i <=strlen ($STR); $i + +) {echo substr ($str,-$i, 1);}}
This program manifest implements two functions, both of which can print the contents of a string in reverse order
The function Reversr_r is implemented recursively, and the function reverse_i () is implemented through loops.