How can PHP be used to calculate and display total costs based on user input selections?

To calculate and display total costs based on user input selections in PHP, you can create a form with input fields for the user to select their choices (such as items to purchase or services to use) and their respective prices. Then, use PHP to process the form data, calculate the total cost based on the selected items and their prices, and display the result to the user.

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $item1 = $_POST['item1'];
    $item2 = $_POST['item2'];
    $item3 = $_POST['item3'];

    $price1 = 10;
    $price2 = 20;
    $price3 = 30;

    $totalCost = ($item1 * $price1) + ($item2 * $price2) + ($item3 * $price3);

    echo "Total cost: $" . $totalCost;
}
?>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    Item 1: <input type="number" name="item1" value="0"><br>
    Item 2: <input type="number" name="item2" value="0"><br>
    Item 3: <input type="number" name="item3" value="0"><br>
    <input type="submit" value="Calculate Total Cost">
</form>