How to recursively return the anonymous function in PHP

Source: Internet
Author: User
PHP 5.2 Added the function of anonymous function, but I found the problem when the anonymous function recursion

 
  0) {        return $test ($a);    }}; $test (10);

As shown in the code above, I want to $test invoke it myself recursively in this anonymous function, but I find that it appears after the call.

Fatal error:function Name must be a string in/library/webserver/documents/test.php on line 9

That is $test , this variable is not recognized, can we have a way to recursively function in the anonymous function itself?

Reply content:

PHP 5.2 Added the function of anonymous function, but I found the problem when the anonymous function recursion

 
  0) {        return $test ($a);    }}; $test (10);

As shown in the code above, I want to $test invoke it myself recursively in this anonymous function, but I find that it appears after the call.

Fatal error:function Name must be a string in/library/webserver/documents/test.php on line 9

That is $test , this variable is not recognized, can we have a way to recursively function in the anonymous function itself?

Need to pass $test as a reference with order for it

$test = function ($a) use (& $test) {    ...}

Reference:http://stackoverflow.com/questions/24 ...

Tips:google helps.

php5.4 down through

$test = NULL, $test = function ($a) use (& $test) {    echo $a;    $a--;    if ($a > 0) {        return $test ($a);    }}; $test (10);
  • 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.