PHP a self-invocation (recursive) case Analysis _php technique based on closed-Package implementation function

Source: Internet
Author: User
Tags anonymous closure php programming php regular expression

This article illustrates the method of PHP's self invocation (recursion) based on the closure implementation function. Share to everyone for your reference, specific as follows:

PHP closures may not be common, but in some cases you might consider using a PHP closure to implement certain functions, such as recursion, where you can implement recursion with PHP closures

PHP Closure implementation function of the call, that is, implementation of recursive function
closure ($n, $counter, $max) {
  //anonymous functions, where the function of the parameter plus & symbol is, the address call parameters themselves
  $FN = Functions (& $n,& $counter,& $max =1) use (& $FN) {//use parameters are passed by the function closure function itself
    $n + +;
    if ($n < $max) {//recursive point, which is the conditional $counter of recursion
      . = $n. ' <br/> ';
      Recursively calls itself
      $fn ($n, $counter, $max);
    return $counter;
  };/ /Remember there must be a '; ' semicolon, no semicolon. PHP will complain, closure function/
  * Here function closure return value is to call the closure of the anonymous function
  * and the closure function, referencing the closure function to pass in the parameters *
  * Return
  $FN ($n, $counter, $max);
Echo (Closure (0, ', 10));

This is a simple use of PHP closure to achieve recursion function, in fact, if we modify the function slightly, can achieve a lot of functions, such as the infinite Polar classification.

On the inside of the closure function, in fact, we can also not refer to the outside of the closure parameter, the inside of the closure function parameters, we can set in the closure.

We can also not return the value of the closure function, because the closure function is in the closure function, we can set the variable in the closure to pass to the closure function, and then, with the closure recursive to get what we want to store in the array, And then use the closure function to return the closure function to get something, so the closure function can also have no return value, the key is what you want to get.

In short, you just use the closure function to implement what you want, and then return what you need, not necessarily from the return value of the closure function.

More about PHP Interested readers can view the site topics: "PHP Search Techniques and Methods Summary", "PHP Data structure and algorithm tutorial", "PHP Programming Algorithm Summary", "PHP encryption Method Summary", "PHP coding and transcoding Operation skills Summary", " Introduction to PHP object-oriented programming program, "PHP Mathematical Calculation Skills Summary", "PHP array Operation techniques Encyclopedia", "PHP string (String) Usage summary", "PHP Regular Expression Usage Summary", and "PHP Common database Operation Skills Summary"

I hope this article will help you with the PHP program design.

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.