Examples of Recursive Implementation in PHP and examples of php Recursion

Source: Internet
Author: User

Examples of Recursive Implementation in PHP and examples of php Recursion

Recursive Definition

Recursion (http:/en.wikipedia.org/wiki/Recursive) is a mechanism of function calling itself (directly or indirectly). This powerful idea can simplify some complex concepts. In addition to computer science, especially in mathematics, recursive concepts are not uncommon. For example, the Fibonacci sequence that is most commonly used for Recursive explanation is an extremely typical example, while other classes such as class (n !) It can also be converted to recursive definitions (n! = N * (n-1 )!). Even in real life, recursive thinking can be seen everywhere: for example, because of academic problems, you need to stamp the principal, but the principal says, "I will only stamp the teaching director ", when you find the teaching director, the teaching director says, "I will only stamp the head of the department "... the process is as follows:

A recursive function is a function that calls itself. Be careful when writing recursive functions, because infinite recursion may occur. Make sure there are sufficient methods to terminate recursion.

1. Use parameter reference to complete recursive functions. The operation is the same memory address.

<? Php $ I = 1; function test (& $ I) {echo $ I; $ I ++; if ($ I <10) {test ($ I );}} test ($ I); // outputs 123456789 test ($ I); // outputs 10?>

2. Use global variables to complete recursive functions.

A real global variable imported using the global statement in the function field is actually a reference to the global variable. In this example, the $ I in the test () function is actually an application of the variable $ I in the first line of the program;

<? Php $ I = 1; function test () {global $ I; echo $ I; $ I ++; if ($ I <10) {test ();}} test (); // output 123456789 test (); // output 10?>

3. Perform recursive functions using static variables.

Static: initializes the variable only when the function is called for the first time, and retains the variable value.

<? Php function test () {static $ I = 1; echo $ I; $ I ++; if ($ I <10) {test () ;}$ I --; // auto-subtraction at the end of each layer of recursion. This sentence can help you understand the execution process of recursive functions} test (); // output 123456789 test (); // output 123456789?>

Example 1. recursively traverse all files in a folder using global variables

function getFiles($dir){global $arr;if(is_dir($dir)){$hadle = @opendir($dir);while($file=readdir($hadle) ){if(!in_array($file,array('.', '..')) ){$dirr = $dir.'/'.$file;if(is_dir($dirr)){getFiles($dirr);}else{array_push($arr, $dirr);}}}}}$arr = array();getFiles('E:/logs');print_r($arr);

Example 2: recursively traverse all files in a folder using static variables

function getFiles ($dir){static $arr = array();if(is_dir($dir)){$hadle = opendir($dir);while($file=readdir($hadle)){if(!in_array($file,array('.','..')) ){$dirr = $dir."/".$file;if(is_dir($dirr)){getFiles ($dirr);}else{array_push($arr,$dirr);}}}}return $arr;}$rows= array();$rows = getFiles ('E:/logs');print_r($rows);

Summary

The above is a detailed description of Recursive Implementation instances in PHP introduced by xiaobian. I hope it will be helpful to you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!

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.