What are the benefits of caching data when creating the Pascal's Triangle recursively in PHP?

When creating Pascal's Triangle recursively in PHP, caching data can greatly improve performance by storing previously calculated values and avoiding redundant calculations. This can help reduce the number of recursive calls and improve overall efficiency of the algorithm.

<?php

function pascalsTriangle($row, $column, $cache = []) {
    if ($column === 0 || $column === $row) {
        return 1;
    }
    
    if (isset($cache[$row][$column])) {
        return $cache[$row][$column];
    }
    
    $result = pascalsTriangle($row - 1, $column - 1, $cache) + pascalsTriangle($row - 1, $column, $cache);
    
    $cache[$row][$column] = $result;
    
    return $result;
}

$row = 5;
for ($i = 0; $i <= $row; $i++) {
    for ($j = 0; $j <= $i; $j++) {
        echo pascalsTriangle($i, $j) . " ";
    }
    echo "\n";
}
?>