Science Stream Practicals

Std 12th IT Subject - Skill Oriented Practical (Science Stream)

SOP 6: Write a PHP to calculate Electricity bill.

SOP 6: Write a program using PHP to calculate Electricity bill by accepting the limits.

Source Code: SOP.php

<!DOCTYPE html>
<html>
<head>
    <title>Electricity Bill Calculator</title>
</head>
<body>
    <h2>Electricity Bill Calculator</h2>
    <form method="post" action="">
        Enter Units Consumed: <input type="number" name="units" required>
        <input type="submit" value="Calculate Bill">
    </form>

    <?php
    function calculateBill($units) {
        $bill = 0;

        if ($units <= 100) {
            $bill = $units * 4;
        } elseif ($units <= 200) {
            $bill = (100 * 4) + (($units - 100) * 5);
        } else {
            $bill = (100 * 4) + (100 * 5) + (($units - 200) * 6);
        }
        
        echo "<h3>Total Units: $units</h3>";
        echo "<h3>Electricity Bill: Rs. $bill</h3>";
    }

    if (isset($_POST["units"])) {
        calculateBill($_POST["units"]);       
    }
    ?>
</body>
</html>

Live Preview