note 121034 added to functions.anonymous
| From: | ayon at hyurl dot com | Date: | Sat, 29 Apr 2017 17:35:09 +0000 |
| Subject: | note 121034 added to functions.anonymous | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-209040@lists.php.net to get a copy of this message | ||
One way to call a anonymous function recursively is to use the USE keyword and pass a reference to
the function itself:
<?php
$count = 1;
$add = function($count) use (&$add){
$count += 1;
if($count < 10) $count = $add($count); //recursive calling
return $count;
};
echo $add($count); //Will output 10 as expected
?>
----
Server IP: 72.52.91.14
Probable Submitter: 47.52.18.124 (proxied: 121.31.251.177)
----
Manual Page -- http://php.net/manual/en/functions.anonymous.php
Edit -- https://master.php.net/note/edit/121034
Del: integrated -- https://master.php.net/note/delete/121034/integrated
Del: useless -- https://master.php.net/note/delete/121034/useless
Del: bad code -- https://master.php.net/note/delete/121034/bad+code
Del: spam -- https://master.php.net/note/delete/121034/spam
Del: non-english -- https://master.php.net/note/delete/121034/non-english
Del: in docs -- https://master.php.net/note/delete/121034/in+docs
Del: other reasons-- https://master.php.net/note/delete/121034
Reject -- https://master.php.net/note/reject/121034
Search -- https://master.php.net/manage/user-notes.php