Konstruktionsplan
1 month ago
402
11
Feedback

Recursive ♻️Factorial

Posted 1 month ago by Konstruktionsplan

Hello again! 👋🏻

This code I have written and understood. The idea came to me when I tried to solve it with a "for" loop.

Is that a "clean code"? I've heard about recursion many times, but until now I didn't call the function again and again, because the thing with the "Allowed memory size" without the if-block scared me! 👻

Thanks! 💜

function recursive_factorial($int) {
  	if ($int == 0) {
	   return 1;
	}
  
	return recursive_factorial($int-1) * $int;
}

print_r(recursive_factorial(5)); // 1 * 2 * 3 * 4 * 5
120

Please sign in or create an account to participate in this conversation.