How can redundant code be avoided in PHP scripts to improve code maintainability?

To avoid redundant code in PHP scripts and improve code maintainability, you can create reusable functions or classes for common tasks instead of repeating the same code multiple times. This way, if you need to make a change to that functionality, you only need to do it in one place, making your code easier to manage and update.

// Example of creating a function to avoid redundant code
function calculateTotal($price, $quantity) {
    return $price * $quantity;
}

// Example of using the function to calculate total cost
$totalCost = calculateTotal(10, 5);
echo "Total cost: $" . $totalCost;