First of all, your undefined constant warnings are because you're NOT quoting your array index names. It should be $_POST['value'] or $_POST["value"], not $_POST[value].
Second, your recusive function doesn't look like it's going to work. See, the body of your function always operates on $_POST['value'], without ever changing the value. Recursive functions should operate on their parameters, and should *ALWAYS* have a base case that exits the function.
You never modify $_POST['value'] during the function's execution, so every time you call that function after submitting a value > 1, you're going to recurse indefinitely. The program will either die becuase the script execution time limit has been reached or you're out of memory.
This is how it should look:
Code:
<?php
function factorial($value)
{
if ($value <= 1) return 1; // see, we're accessing a local variable,
// $value, not the actual value passed in
// by the user.
else return $value * factorial($value - 1);
}
echo "The factorial of {$_POST['value']} is " . factorial($_POST['value']);
?>
There's no reason a utility function should know or care about data submitted by a user, because you limit the usefulness of your function. The function calculates factorials. It doesn't care where that number comes in from.
It just so happens that in this case, the value originally came from user input. Using the function I've defined, you're not limited to this. For example:
$four_fact = factorial(4);
You wouldn't be able to do the above if you hard-coded $_POST['value'] in your function implementation.
Also, your function is poorly named -- the function calculates factorials, let that decide what you name it. Sure, it uses recursion to do it, but that's not anything that a USER of the function should know about. For example, you don't care how join() works internally, you just know what it accepts as parameters and what it returns as a value.
Take care,
Nik
http://www.bigaction.org/