How does the use of recursion play a role in the script's functionality?

Recursion is used in the script to repeatedly call a function within itself until a certain condition is met. This allows for a more concise and elegant solution to problems that involve repetitive tasks or nested data structures. In this case, recursion can be used to traverse a nested array or perform a repetitive calculation.

function calculateFactorial($n) {
    if ($n <= 1) {
        return 1;
    } else {
        return $n * calculateFactorial($n - 1);
    }
}

$number = 5;
echo "Factorial of $number is: " . calculateFactorial($number);