PHP Program to calculate Electricity Bill using ‘if’ statement.
How does this program work?
- This code helps to calculate Electricity Bill using ‘if’ statement.
Here is the code
<head>
<title> Power Bill</title>
</head>
<body>
<form method="post">
<table border="0">
<tr>
<td>Enter Meter Number:
<input type="number" name="m" value="" placeholder="Enter m value"/>
</td>Enter Number of Units: </tr>
<tr>
<td> <input type="number" name="u" value="" placeholder="Enter u value"/> </td>
</tr>
<tr>
<td>
Enter Category:
<select name="c">
<option>Select </option>
<option>Agriculture </option>
<option>Residential </option>
<option>Commercial </option>
</select>
</td>
</tr> <td> <input type="submit" name="btn_submit" value="Submit"/> </td>
</tr>
</table>
</form>
<?php
if(isset($_POST['btn_submit']))
{
$m = $_POST['m'];
$u = $_POST['u'];
$c = $_POST['c'];
if($c=="Agriculture")
{
if($u>0&&$u<=50)
{
$e = 20;
$price = (($u*.25)+$e); }
else if($u>50&&$u<=100) {
$e = 20;
$price = (($u*.55)+$e); }
else if($u>100&&$u<=150) {
$e = 20;
$price = (($u*0.80)+$e); }
else if($u>150&&$u<=250) {
$e = 20;
$price = (($u*1.50)+$e); }
else if($u>250&&$u<=400) {
$e = 20;
$price = (($u*2)+$e); }
else if($u>400) {
$e = 20;
$price = (($u*2.5)+$e); }
echo "Your Meter Number is: " .$m ;
echo "Units Are: " .$u ;
echo "Extra Charges Are: " .$e ;
echo "Total $u Units of Charges: ". $price;
}
else if($c=="Residential")
{
if($u>0&&$u<=50) {
$e = 35;
$price = (($u*1.35)+$e); }
else if($u>50&&$u<=100) {
$e = 35;
$price = (($u*2.15)+$e); }
else if($u>100&&$u<=150) {
$e = 35;
$price = (($u*3)+$e); }
else if($u>150&&$u<=250) {
$e = 35;
$price = (($u*3.5)+$e); }
else if($u>250&&$u<=400) {
$e = 35;
$price = (($u*4)+$e); }
else if($u>400) {
$e = 35;
$price = (($u*6)+$e); }
echo "Your Meter Number is: " .$m;
echo "Units Are: " .$u ;
echo "Extra charges are: " .$e;
echo "Total $u Units of Charges: ". $price;
}
else if($c=="Commercial")
{
if($u>0&&$u<=50) {
$e = 65;
$price = (($u*3)+$e); }
else if($u>50&&$u<=100) {
$e = 65;
$price = (($u*4.5)+$e); }
else if($u>100&&$u<=150) {
$e = 65;
$price = (($u*5.5)+$e); }
else if($u>150&&$u<=250) {
$e = 65;
$price = (($u*6.8)+$e); }
else if($u>250&&$u<=400) {
$e = 65;
$price = (($u*7.2)+$e); }
else if($u>400) {
$e = 65;
$price = (($u*9)+$e);
}
echo "Your Meter Number is: " .$m ;
echo "Units Are: " .$u;
echo "Extra Charges Are: " .$e ;
echo "Total $u Units of Charges: ".price;
}
else
{
echo "Invalid Number"; }
}
?>
</body>
</html>