In what situations should comments be added to PHP code for better understanding and maintenance?

Comments should be added to PHP code in situations where the code is complex or difficult to understand, when there are workarounds or temporary solutions implemented, or when there are specific requirements or constraints that need to be documented for future maintenance. Comments can also be helpful when explaining the purpose or functionality of certain code blocks or variables.

// This function calculates the total price of items in the shopping cart
function calculateTotalPrice($items) {
    $totalPrice = 0;
    
    // Loop through each item in the shopping cart and add up the prices
    foreach ($items as $item) {
        $totalPrice += $item['price'];
    }
    
    // Apply a 10% discount if the total price is over $100
    if ($totalPrice > 100) {
        $totalPrice *= 0.9; // Apply 10% discount
    }
    
    return $totalPrice;
}