How can arrays be used to calculate invoices directly in PHP?

To calculate invoices directly in PHP using arrays, you can create an array to store the prices of each item and another array to store the quantities. Then, loop through these arrays to calculate the total cost by multiplying the price of each item by its quantity and summing them up. Finally, you can display the total cost on the invoice.

<?php
// Define arrays for prices and quantities of items
$prices = [10, 20, 30];
$quantities = [2, 3, 1];

// Calculate total cost by multiplying price by quantity for each item
$totalCost = 0;
for ($i = 0; $i < count($prices); $i++) {
    $totalCost += $prices[$i] * $quantities[$i];
}

// Display the total cost on the invoice
echo "Total Cost: $" . $totalCost;
?>