In what situations would recursion be a more suitable approach than nested for loops for generating string combinations in PHP?

Recursion would be a more suitable approach than nested for loops for generating string combinations in PHP when the number of nested loops needed is not known beforehand or when the depth of nesting is variable. Recursion can handle these dynamic situations more gracefully and with less code complexity compared to nested loops.

function generateCombinations($input, $prefix = '') {
    $n = strlen($input);
    if ($n == 0) {
        echo $prefix . "\n";
    } else {
        for ($i = 0; $i < $n; $i++) {
            generateCombinations(substr($input, 0, $i) . substr($input, $i + 1), $prefix . $input[$i]);
        }
    }
}

$input = "abc";
generateCombinations($input);