What are common pitfalls when using functions in PHP, and how can they be avoided?

One common pitfall when using functions in PHP is not properly handling errors or exceptions within the function itself. To avoid this, always include error handling mechanisms such as try-catch blocks or error reporting within your functions.

function divide($numerator, $denominator) {
    try {
        if ($denominator == 0) {
            throw new Exception("Division by zero");
        }
        return $numerator / $denominator;
    } catch (Exception $e) {
        echo "Error: " . $e->getMessage();
    }
}

// Example usage
echo divide(10, 0); // Output: Error: Division by zero