Search
 
SCRIPT & CODE EXAMPLE
 

PHP

how to write a php program for an electricity bill using if-else conditions

<!DOCTYPE html>
<html>
<head>
	<title>Program to calculate electricity bill in PHP</title>
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
	<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
</head>
<body>
	<?php
	$amount = '';
	$kwh_usage = '';
	if (isset($_POST['submit'])) {
		$units = $_POST['kwh'];
		if (!empty($units)) {
			$kwh_usage = calculate_electricity_bill($units);
			$amount = '<strong>Total amount of ' . $units . ' units -</strong> ' . $kwh_usage;
		}
	}
	/**
	 * To calculate electricity bill as per unit cost
	 */
	function calculate_electricity_bill($units) {
		$first_unit_cost = 8;
		$second_unit_cost = 12;
		$third_unit_cost = 16;
		$fourth_unit_cost = 20;

		if($units <= 100) {
			$bill = $units * $first_unit_cost;
		}
		else if($units > 100 && $units <= 200) {
			$temp = 100 * $first_unit_cost;
			$remaining_units = $units - 100;
			$bill = $temp + ($remaining_units * $second_unit_cost);
		}
		else if($units > 200 && $units <= 300) {
			$temp = (100 * $first_unit_cost) + (100 * $second_unit_cost);
			$remaining_units = $units - 200;
			$bill = $temp + ($remaining_units * $third_unit_cost);
		}
		else {
			$temp = (100 * $first_unit_cost) + (100 * $second_unit_cost) + (100 * $third_unit_cost);
			$remaining_units = $units - 300;
			$bill = $temp + ($remaining_units * $fourth_unit_cost);
		}
		return number_format((float)$bill, 2, '.', '');
	}
	?>
	<div class="container">
		<h1>Calculate electricity bill in PHP</h1>
		<div class="form-group">
		<form action="" method="post">
		<div class="form-group">
			<input type="number" name="kwh" placeholder="Please enter no. of Units" class="form-control" />
		</div>
		<div class="form-group">		
			<input type="submit" name="submit" class="btn btn-primary" value="Submit" />
		</div>	
		</form>
		</div>
		<div>
		    <?php echo '<br />' . $amount; ?>
		</div>
	</div>
</body>
</html>
Comment

how to write a php program for an electricity bill using if-else conditions

<!DOCTYPE html>
<html>
<head>
	<title>Program to calculate electricity bill in PHP</title>
	<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
	<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
</head>
<body>
	<?php
	$amount = '';
	$kwh_usage = '';
	if (isset($_POST['submit'])) {
		$units = $_POST['kwh'];
		if (!empty($units)) {
			$kwh_usage = calculate_electricity_bill($units);
			$amount = '<strong>Total amount of ' . $units . ' units -</strong> ' . $kwh_usage;
		}
	}
	/**
	 * To calculate electricity bill as per unit cost
	 */
	function calculate_electricity_bill($units) {
		$first_unit_cost = 8;
		$second_unit_cost = 12;
		$third_unit_cost = 16;
		$fourth_unit_cost = 20;

		if($units <= 100) {
			$bill = $units * $first_unit_cost;
		}
		else if($units > 100 && $units <= 200) {
			$temp = 100 * $first_unit_cost;
			$remaining_units = $units - 100;
			$bill = $temp + ($remaining_units * $second_unit_cost);
		}
		else if($units > 200 && $units <= 300) {
			$temp = (100 * $first_unit_cost) + (100 * $second_unit_cost);
			$remaining_units = $units - 200;
			$bill = $temp + ($remaining_units * $third_unit_cost);
		}
		else {
			$temp = (100 * $first_unit_cost) + (100 * $second_unit_cost) + (100 * $third_unit_cost);
			$remaining_units = $units - 300;
			$bill = $temp + ($remaining_units * $fourth_unit_cost);
		}
		return number_format((float)$bill, 2, '.', '');
	}
	?>
	<div class="container">
		<h1>Calculate electricity bill in PHP</h1>
		<div class="form-group">
		<form action="" method="post">
		<div class="form-group">
			<input type="number" name="kwh" placeholder="Please enter no. of Units" class="form-control" />
		</div>
		<div class="form-group">		
			<input type="submit" name="submit" class="btn btn-primary" value="Submit" />
		</div>	
		</form>
		</div>
		<div>
		    <?php echo '<br />' . $amount; ?>
		</div>
	</div>
</body>
</html>
Comment

PREVIOUS NEXT
Code Example
Php :: add tag tpo protfolio? 
Php :: how to decode json and combine again in php 
Php :: laravel collection flip 
Php :: update request php-salesforce-rest-api 
Php :: checnge message no products were found matching your selection woocommerce edit 
Php :: Combining AND, OR and NOT 
Php :: print array blade laravel 
Php :: symfony 6 download 64 bit 
Php :: event handler with worker laravel 
Php :: php email 
Php :: laravel find query 
Php :: heroku mysql 
Php :: php associative array 
Php :: php boolean 
Php :: how to print any string in double quotes in php 
Php :: http_build_query 
Php :: get action name in yii2 
Php :: str_word_count() 
Php :: schema add column laravel 
Java :: vm options javafx 
Java :: Manifest merger failed : Apps targeting Android 12 and higher are required to specify an explicit value for `android:exported` 
Java :: save map to file java 
Java :: javafx button color 
Java :: calculate age in days java 
Java :: java stream collect to arraylist 
Java :: string to double java exception 
Java :: gravatar default 
Java :: java how to find length of int 
Java :: java ip regex pattern 
Java :: how to check type of primitive value in java 
ADD CONTENT
Topic
Content
Source link
Name
8+3 =