Factorial in PHP

Example for versions PHP 5.2.4, PHP 5.3.2

This example uses recursive factorial definition.

<?php
function factorial($n)
{
    if ($n == 0) {
        return 1;
    }
        
    return $n * factorial($n - 1);
}

for ($n = 0; $n <= 16; $n++) {
    echo $n . "! = " . factorial($n) . "\n";
}
?>