What are the best practices for implementing recursive functions in PHP for Collatz sequences?

When implementing recursive functions in PHP for Collatz sequences, it is important to define a base case to end the recursion and handle both odd and even numbers correctly. The function should call itself with the appropriate next number in the sequence until the base case is reached.

function collatz($n) {
    echo $n . " ";
    
    if ($n == 1) {
        return;
    } else if ($n % 2 == 0) {
        collatz($n / 2);
    } else {
        collatz(3 * $n + 1);
    }
}

// Example usage
collatz(6);