What potential pitfalls can arise when setting default values for variadic parameters in PHP functions?

Setting default values for variadic parameters in PHP functions can lead to unexpected behavior if the default value is an array. This is because PHP treats the default value as a single parameter, rather than individual values for each variadic argument. To solve this issue, you can use null as the default value and manually check if the argument is null to assign a default array value inside the function.

function exampleFunction(...$args) {
    $defaultArray = [1, 2, 3];
    
    foreach ($args as $arg) {
        if ($arg === null) {
            $arg = $defaultArray;
        }
        
        // Perform actions with $arg
    }
}

// Usage
exampleFunction(4, null, 6); // $args will be [4, [1, 2, 3], 6]