Can php function expressions be recursive?

Source: Internet
Author: User
How does one implement recursion with a function expression {code? The {code...} clause is not very elegant. {code...} has a function expression.

$make = function($std){};

How to Implement recursion?

$make = function($std){ $make($std);  };

It is not very elegant to exclude such writing.

$make = function($std, $make){ $make($std);  };$make($std, $make);

Reply content:

There is a function expression

$make = function($std){};

How to Implement recursion?

$make = function($std){ $make($std);  };

It is not very elegant to exclude such writing.

$make = function($std, $make){ $make($std);  };$make($std, $make);

$f = function($x) use(&$f){    if($x==0 || $x==1)        return 1;    return $f($x-1) + $f($x-2);};print $f(10);

I think the main idea must be something like this. Note,use(&$f)References must be used here; otherwise, errors may occur.

Use can beWhen defining anonymous FunctionsTo capture external variables (copying values) into an anonymous function.
In this example, $ f can be considered as nonexistent when the assignment is not completed. In this case, an undefined variable is captured. Therefore, we need to capture a reference. After the $ f value is assigned, $ f inside the anonymous function points to the anonymous function itself.
(This is too difficult ...)


  

This Fibonacci recursive computation is a reference for you.

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.