PHP Exercise : 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;


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

	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!");
		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.

Related PHP Programs for Practice

PHP - Division table program
PHP - Prime Number Program
PHP - Print numbers 10 to 1 using recursion
PHP - Loop through an associative array
PHP - Differentiate between fgets, fgetss and fgetcsv
PHP - Set session on login
PHP - Convert text to speech
PHP - Copying or moving a file
PHP - Locking a file
PHP - CURL Cookie Jar
PHP - Password Hashing
PHP - Emoji Unicode Characters PHP - Password Hashing
PHP - Emoji Unicode Characters
PHP - Age calculator
PHP - Get array key
PHP - Capitalize first letter
PHP - Set Timezone
PHP - Remove duplicates from array
PHP - Calculate percentage of total
PHP - Fibonacci Series Program
PHP - Insert image in database

Read more articles

General Knowledge

Learn Popular Language