How can PHP developers improve code readability and formatting to enhance collaboration and maintainability?

To improve code readability and formatting in PHP, developers can adhere to coding standards such as PSR-1 and PSR-2, use meaningful variable names, properly indent code blocks, and add comments to explain complex logic. This will enhance collaboration among team members and make the codebase more maintainable in the long run.

<?php

// Example of well-formatted and readable PHP code
function calculateTotal($price, $quantity) {
    // Calculate the total cost
    $total = $price * $quantity;

    // Apply a discount if quantity is greater than 10
    if ($quantity > 10) {
        $discount = $total * 0.1;
        $total -= $discount;
    }

    return $total;
}

// Usage
$totalCost = calculateTotal(20, 15);
echo "Total cost is: $totalCost";

?>