Php recursive use example (php recursive function) _ php instance

Source: Internet
Author: User
This article mainly introduces php recursive examples (php recursive functions), including recursively obtaining the role ID string, recursively obtaining the cascade role information array, and obtaining the subrole information through the parent role id, for more information, see
// Recursively obtain the role ID string function explodeRole ($ roleObj, & $ resultStr) {if (0 <count ($ roleObj-> childRoleObjArr )) {foreach ($ roleObj-> childRoleObjArr as $ childRoleObj) {if (''= $ resultStr) {$ resultStr. = "{$ childRoleObj-> id}";} else {$ resultStr. = ", {$ childRoleObj-> id}";} explodeRole ($ childRoleObj, $ resultStr );}}} // recursively retrieve the cascade role information Array function makeRoleRelation (& $ roleObjArr) {foreach ($ roleObjArr as $ item) {$ item-> chil DRoleObjArr = getRoleObjArrByParentId ($ item-> id); if (0 <count ($ item-> childRoleObjArr) {makeRoleRelation ($ item-> childRoleObjArr );}}} // obtain the subrole information using the parent role id. function getRoleObjArrByParentId ($ parentid) {$ operCOGPSTRTSysRole = new COGPSTRTSysRole (); $ response-> setColumn ($ operCOGPSTRTSysRole-> getAllColumn (); $ operCOGPSTRTSysRole-> setWhere ("parentroleid = {$ parentid}"); $ roleObjArr = $ operCOGPST RTSysRole-> convResult2ObjArr ($ operCOGPSTRTSysRole-> selectTable (); return isset ($ roleObjArr )? $ RoleObjArr: array ();}

Php recursive function usage

A function is called recursively in its own body. This type of function is called a recursive function. This is usually of high practical value for programmers. It is often used to break down complicated problems into simple and identical situations, and repeatedly process these problems until the problem is solved.

Difference between recursive functions and non-recursive functions

Example 1: use static variables

function test(){  static $dig=0;  if($dig++<10){    echo $dig;    test();  }}test();//12345678910

Example 2: Use recursive functions and loops to reverse string Arrangement

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 can be replaced cyclically in many cases. We recommend that you use them when we cannot replace them with loops, because loops are more understandable and error-prone.

Php recursive functions: php payment recursive functions. recursive functions call themselves. These functions are especially suitable for browsing dynamic data structures, such as trees and lists.
Almost no web applications require complex data structures

<?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);}}

Two functions are implemented in this program list. Both functions can print the content of the string in reverse order.
The reversr_r function is implemented through recursion, while the reverse_ I () function is implemented through loops.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.