EDIT: I'm sorry for the duplicate, but searching for the title of this question wasn't showing such duplicate in the search result, so I was not aware there is question, already.
This won't work, because $greet
is unknown by the time it will be called.
<?php
$greet = function($name)
{
printf("Hello %s\r\n", $name);
if($name != 'PHP')
{
$greet('PHP'); // $greet not defined
}
};
$greet('World');
?>
The idea is to have a recursive function that is purely in its parent scope (i.e. function in function), without the requirement to build a class.
So, how to build an anonymous recursive function in PHP, properly? Is it even possible? If so, how?