Xin Xing and your interpretation of PHP recursion

Source: Internet
Author: User
Xin Xing and your interpretation of PHP recursion & nbsp; actually, recursion in other programming languages may be a exercise for beginner functions, but due to the particularity of PHP, let's take it out and explain it in detail. First, let's talk about what recursion means. I first realized that recursion is a factorial of a number. for example, if we write a function, then, calculate the factorial value. & Nbsp; check the following PHP code: & lt ;? Php evaluate n! Fu Xinxing and your interpretation of PHP recursion

Actually, recursion may be an exercise for beginners in other programming languages. However, due to the special nature of PHP, let's take it out and explain it in detail. First, let's talk about what recursion means, I first knew that recursion is to find a factorial of a number. for example, we write a function and then calculate its factorial.

See the following PHP code:

 0) {return $ n * xin ($ n-1);} else {return 1 ;}} echo xin (4 );
The output result is of course 24. what does it mean? it is very simple. we pass a value to the xin function. if it is larger than 0, then multiply it by the result of calling the $ n-1 xin function. of course, the reader may have N ways to calculate the n factorial, but no doubt, this is calculated using recursion.

It can be seen that the recursion in PHP is similar to the mathematical induction method of high school learning. The principle is very simple and easy to understand. The general step is to first determine whether or not downward recursion is required. if so, a rule is provided. if not, a timely response is required to give control to its caller, there are so many things about the basic principles. write them 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.