PHP Programs

Please click on 1-2 Ads to keep this website's Study Material FREE and regularly Updated.

Program 1: Write a function to calculate the factorial of a number (non-negative integer). The function accept the number as an argument.

factorial.php

<!DOCTYPE html>
<html>
<head>
	<title>PHP Programs TutorialsDuniya.com</title>
</head>
<body>
	<form name="Factorial Calculator" method="POST" action="">
		Enter any number to calculate factorial
		<input type="text" name="input"><br/>
		<input type="submit" name="submit" value="Calculate">
	</form>

	<?php
		if(isset($_POST["submit"]) == "Calculate")
		{
			$number=$_POST["input"];
			$fact = calculate_factorial($number);
			echo"<br/> Factorial of $number is $fact";
		}

		function calculate_factorial($num)
		{
			$factorial = 1;

			for($x=$num; $x>=1; $x--)
			{
				$factorial=$factorial*$x;
			}
			return $factorial;
		}
	?>
</body>
</html>



If you have any suggestion or found any error in the above program, then do let us know at [email protected] or just comment below.