Q:

There are two deals of an item to buy. The quantities and prices of the item are given below. Write a program in PHP to find the best deal to purchase the item

belongs to collection: PHP Programming Exercises

0

PHP Arithmetic and Comparison

There are two deals of an item to buy. The quantities and prices of the item are given below. Write a program in PHP to find the best deal to purchase the item

$quantity1 = 70;

$quantity2 = 100;

$price1 = 1035;

$price2 = 1200;

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Solution

The following code, helps to decide which quantity of item is best to purchase or which deal is fruitful.

<?php  
	function better_deal($quantity1, $quantity2, $price1, $price2) 
	{
		$deal1 = $price1 / $quantity1;
		$deal2 = $price2 / $quantity2;
		return($deal1 < $deal2);
	}
	
	$quantity1 = 70;
	$quantity2 = 100;
	$price1 = 1035;
	$price2 = 1200;
	
	if(better_deal($quantity1, $quantity2, $price1, $price2)) {
		print("The first deal is best!");
	}
	else 
	{
		print("The second deal is best!");
	}
?>

Output of the above code

The second deal is best!

In the above code, better_deal() function takes the four values as arguments and perform the arithmetic and comparison in just three lines of code and it returns the value of a boolean expression. We have embedded the returned boolean value in IF statement and gets the best deal. As we have written the arithmetic and comparison codes within function, so we can use this in multiple places, only we have to change the values and calculations.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

PHP Programming Exercises

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write a php program to set session on successful l... >>
<< Write a php program to differentiate between fgets...