How can PHP be used to efficiently calculate discounts based on specific conditions, such as total purchase amount?

To efficiently calculate discounts based on specific conditions in PHP, you can use conditional statements to check the total purchase amount and apply the appropriate discount percentage. By using if-else or switch statements, you can easily determine the discount amount based on the conditions set.

$totalPurchaseAmount = 100; // Example total purchase amount
$discount = 0; // Initialize discount variable

if ($totalPurchaseAmount >= 100) {
    $discount = 10; // Apply 10% discount for total purchase amount over or equal to 100
} elseif ($totalPurchaseAmount >= 50) {
    $discount = 5; // Apply 5% discount for total purchase amount over or equal to 50
} else {
    $discount = 0; // No discount for total purchase amount less than 50
}

echo "Discount percentage: " . $discount . "%";