The function is "recursive", in that it calls itself. Each time it calls itself, it does so with the number it was previously called with being reduced by 1. So when you call the function as factorial(5), it start out on the first iteration taking the input value (5) and multiplying it by the result of factorial(4) (4 being $n -1 ). That call to factorial() is now working with the value of 4, so it calls itself again with the parameter being 3, and so on until the value gets down to 0. So the end result of that initial call to factorial(5) is 5 4 3 2 1, or 120 if I did the arithmetic correctly in my head.
To demonstrate this, you could add a little output to the function:
<?php
function factorial ($n) {
echo $n."<br />\n";
if ($n==0) {
return 1;
}
else {
return $n*factorial($n-1);
}
}
print ("5! = ".factorial(5));
?>
This results it: