Sinsing and you interpret the recursion of PHP

Source: Internet
Author: User
Sinsing and your interpretation of PHP recursion

In fact, recursion into other programming languages may be a beginner's function of an exercise, but because of the particularity of PHP, we take it out specifically to explain, first of all, what is recursion, I first know recursion is to find a number of factorial, such as we write a function, and then ask its factorial is how much.

Look at the following PHP code:

 
 0) {return $n *xin ($n-1);} Else{return 1;}} Echo Xin (4);
Its output is of course 24, what do you mean, very simple, we give a value to the Xin function, if it is larger than 0, then use it to multiply the result of calling $n-1 's Xin function, of course, the reader may have n ways to calculate the factorial of n calculation, but undoubtedly, this is a recursive calculation.

As can be seen, the recursion in PHP is very similar to high school learning mathematical induction, the principle is very simple and understandable. Its approximate step is to first determine whether the need to recursive, if so, give a rule, if not, need to return in a timely manner, give control to its callers, the basic principle is so much, there is something to write here first.


  • 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.