Recursive Function

PHP Programming

PHP Programming Examples

<!DOCTYPE html>
<html>
	<head>
		<title>Program for Recursive Factorial</title>
	</head>
	<body>
		<form method="post" action="">
			<input type="text" name="data" placeholder="Enter a Number to calculate Factorial" />
			<input type="submit" value="Calcualte" />
		</form>
		<br />
		<?php
			if(isset($_POST['data'])) {
				
				/* Recursive function */
				function factorial($no) {
					if($no == 1)
						return 1;
					else
						/* Call the factorial function inside in it */
						return($no * factorial($no - 1));
				}
	
				/* Call user define function with parameter */
				echo "Factorial value is: ".factorial($_POST['data']);
			}
		?>
	</body>
</html>


          /****** Output ******/
          

Input:


5




Factorial value is: 120

Learn more about user define function