Lambda recursive PHP functions.

Posted by Kendall Hopkins on Stack Overflow See other posts from Stack Overflow or by Kendall Hopkins
Published on 2010-03-19T19:51:51Z Indexed on 2010/03/19 20:41 UTC
Read the original article Hit count: 180

Is it possible to have a PHP function that is both recursive and anonymous (lambda). This is my attempt to get it to work, but it doesn't pass in the function name.

$factorial = function( $n ) use ( $factorial ) {
    if( $n == 1 ) return 1;
    return $factorial( $n - 1 ) * $n;
};
print $factorial( 5 );

I'm also aware that this is a bad way to implement factorial, it's just an example.

© Stack Overflow or respective owner

Related posts about php

Related posts about lambda