How can PHP be used to calculate the total cost based on user input from a form?

To calculate the total cost based on user input from a form in PHP, you can retrieve the input values using $_POST or $_GET superglobals, perform any necessary calculations, and then display the result to the user.

<?php
if($_SERVER["REQUEST_METHOD"] == "POST") {
    $quantity = $_POST['quantity'];
    $price = $_POST['price'];
    
    $totalCost = $quantity * $price;
    
    echo "Total Cost: $" . $totalCost;
}
?>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    Quantity: <input type="text" name="quantity"><br>
    Price: <input type="text" name="price"><br>
    <input type="submit" value="Calculate Total Cost">
</form>