<?php
/**
 * Calculates a factorial of given number.
 * @param string|int $num
 * @throws InvalidArgumentException
 * @return string
 * Thanks to https://www.php.net/manual/en/book.bc.php
 * RJM Programming ... May, 2024
 */

set_time_limit(9600);

function bcfact($num) {
    if (!filter_var($num, FILTER_VALIDATE_INT) || $num <= 0) {
        throw new InvalidArgumentException(sprintf('Argument must be natural number, "%s" given.', $num));
    }

    for ($result = '1'; $num > 0; $num--) {
        $result = bcmul($result, $num);
    }

    return $result;
}

$prefix="<html><head><title>Factorial Fun - RJM Programming - May, 2024 ... thanks to https://www.php.net/manual/en/book.bc.php</title></head><body><h1>Factorial Fun</h1><h3>RJM Programming - May, 2024</h3><h4>Thanks to <a target=_blank title='Home of PHP' href='https://www.php.net/manual/en/book.bc.php'>https://www.php.net/manual/en/book.bc.php</a></h4><br>";
$midbit='';
$suffix="<br><br><form action=./use_bcmath.php method=GET><span>Factorial of: <input type=number style=width:30%; name=ofwhat min=1 max=67564 step=1 value=1></input><br><input type=submit value=Calculate></input></form></body></html>";

if (isset($_GET['ofwhat'])) {
 if (strlen($_GET['ofwhat']) > 0) {
   $midbit='<br><table style=width:95%;><tr><td style=vertical-align:top;text-align:right;>Factorial of ' . str_replace('+',' ',urldecode($_GET['ofwhat']));
   $midbit.=' is </td><td><textarea style="display:inline-block;border:5px solid lightgreen;width:50%;height:300px;">' . bcfact(str_replace('+',' ',urldecode($_GET['ofwhat']))) . '</textarea></td></tr></table><br>';
 }
}

echo $prefix . $midbit . $suffix;
exit;
?>