PHP provides built-in mathematical functions that make calculations easier and faster. These functions are useful for calculating discounts, finding square roots, rounding prices, comparing values, and working with percentages. In this chapter, you will practice important Mathematical Functions in PHP through solved questions with practical examples, PHP code, sample outputs, and beginner-friendly explanations.
Question 1: Find the Absolute Value of a Number Using abs()
Problem Statement
Write a PHP program to find the absolute value of -250 using the abs() function.
PHP Solution
<?php
$number = -250;
$result = abs($number);
echo "Absolute Value: " . $result;
?>
Sample Output
Absolute Value: 250
Explanation
The abs() function returns the positive value of a number, regardless of whether the original number is positive or negative.
For example:
abs(-250)returns250.abs(250)returns250.
Concepts Covered
abs()function- Absolute values
- Positive and negative numbers
Question 2: Calculate the Square Root Using sqrt()
Problem Statement
A company has a square storage area with an area of 144 square meters. Write a PHP program to calculate the length of one side using the sqrt() function.
PHP Solution
<?php
$area = 144;
$side = sqrt($area);
echo "Length of One Side: " . $side . " meters";
?>
Sample Output
Length of One Side: 12 meters
Explanation
The sqrt() function calculates the square root of a number.
Since:
[
12 \times 12 = 144
]
The square root of 144 is 12. Therefore, the length of one side is 12 meters.
Concepts Covered
sqrt()function- Square root calculation
- Mathematical operations
Question 3: Calculate Power Using pow()
Problem Statement
Write a PHP program to calculate (5^3) using the pow() function.
PHP Solution
<?php
$base = 5;
$exponent = 3;
$result = pow($base, $exponent);
echo "Result: " . $result;
?>
Sample Output
Result: 125
Explanation
The pow() function calculates the power of a number.
Its syntax is:
pow($base, $exponent);
Here, PHP calculates:
[
5 \times 5 \times 5 = 125
]
Concepts Covered
pow()function- Base and exponent
- Power calculation
Question 4: Round a Number Up Using ceil()
Problem Statement
A shopping website calculates a delivery charge of ₹125.35. Write a PHP program to round this amount up to the next whole number using ceil().
PHP Solution
<?php
$deliveryCharge = 125.35;
$result = ceil($deliveryCharge);
echo "Delivery Charge: ₹" . $result;
?>
Sample Output
Delivery Charge: ₹126
Explanation
The ceil() function always rounds a number upward to the nearest integer.
Examples:
ceil(125.35)returns126.ceil(125.01)returns126.ceil(125.00)returns125.
Concepts Covered
ceil()function- Rounding upward
- Decimal numbers
Question 5: Round a Number Down Using floor()
Problem Statement
An employee works for 8.75 hours. Write a PHP program to round the working hours down to the nearest whole number using floor().
PHP Solution
<?php
$workingHours = 8.75;
$result = floor($workingHours);
echo "Completed Working Hours: " . $result;
?>
Sample Output
Completed Working Hours: 8
Explanation
The floor() function rounds a number downward to the nearest integer.
Examples:
floor(8.75)returns8.floor(8.99)returns8.floor(8.00)returns8.
Unlike ceil(), which rounds upward, floor() rounds downward.
Concepts Covered
floor()function- Rounding downward
- Decimal calculations
Question 6: Round a Decimal Number Using round()
Problem Statement
A product has a calculated price of ₹1499.756. Write a PHP program to round the price to two decimal places using round().
PHP Solution
<?php
$productPrice = 1499.756;
$result = round($productPrice, 2);
echo "Rounded Price: ₹" . number_format($result, 2);
?>
Sample Output
Rounded Price: ₹1,499.76
Explanation
The round() function rounds a number to the nearest value.
The second argument specifies the number of decimal places.
Here, round($productPrice, 2) rounds the price to two decimal places. number_format() displays the result with two decimal digits and a thousands separator.
Concepts Covered
round()function- Decimal precision
number_format()- Price calculations
Question 7: Find the Highest Value Using max()
Problem Statement
A company records the sales of three employees:
- Amit: ₹45,000
- Neha: ₹58,000
- Rahul: ₹52,000
Write a PHP program to find the highest sales amount using max().
PHP Solution
<?php
$amitSales = 45000;
$nehaSales = 58000;
$rahulSales = 52000;
$highestSales = max($amitSales, $nehaSales, $rahulSales);
echo "Highest Sales: ₹" . number_format($highestSales);
?>
Sample Output
Highest Sales: ₹58,000
Explanation
The max() function returns the largest value from the given numbers.
Here, PHP compares the three sales amounts and returns 58000, which is Neha’s sales amount.
Concepts Covered
max()function- Comparing numeric values
- Finding the highest sales
Question 8: Find the Lowest Value Using min()
Problem Statement
A store records the prices of three products:
- Keyboard: ₹850
- Mouse: ₹450
- Headphones: ₹1,200
Write a PHP program to find the lowest product price using min().
PHP Solution
<?php
$keyboard = 850;
$mouse = 450;
$headphones = 1200;
$lowestPrice = min($keyboard, $mouse, $headphones);
echo "Lowest Product Price: ₹" . number_format($lowestPrice);
?>
Sample Output
Lowest Product Price: ₹450
Explanation
The min() function returns the smallest value from the supplied values.
PHP compares all three product prices and returns 450, which is the price of the mouse.
Concepts Covered
min()function- Comparing values
- Finding the lowest price
Question 9: Calculate the Circumference of a Circle Using pi()
Problem Statement
Write a PHP program to calculate the circumference of a circle with a radius of 7 centimeters using the pi() function.
Formula:
[
C = 2 \times \pi \times r
]
PHP Solution
<?php
$radius = 7;
$circumference = 2 * pi() * $radius;
echo "Circumference: " . round($circumference, 2) . " cm";
?>
Sample Output
Circumference: 43.98 cm
Explanation
The pi() function returns the mathematical value of π (approximately 3.14159).
The circumference formula is:
[
C = 2 \times 3.14159 \times 7
]
The result is approximately 43.98 centimeters after rounding to two decimal places.
Concepts Covered
pi()function- Circle circumference
- Mathematical formulas
round()
Question 10: Calculate a Discounted Product Price Using Mathematical Functions
Problem Statement
An online store offers a 15% discount on a product priced at ₹2,499.75. Calculate the discount amount and final payable price. Round the final price to two decimal places.
PHP Solution
<?php
$productPrice = 2499.75;
$discountRate = 15;
$discountAmount = ($productPrice * $discountRate) / 100;
$finalPrice = round($productPrice - $discountAmount, 2);
echo "Original Price: ₹" . number_format($productPrice, 2) . "\n";
echo "Discount Amount: ₹" . number_format($discountAmount, 2) . "\n";
echo "Final Price: ₹" . number_format($finalPrice, 2);
?>
Sample Output
Original Price: ₹2,499.75
Discount Amount: ₹374.96
Final Price: ₹2,124.79
Explanation
First, PHP calculates 15% of the original product price:
[
\frac{2499.75 \times 15}{100} = 374.9625
]
Next, the discount amount is subtracted from the original price:
[
2499.75 – 374.9625 = 2124.7875
]
Finally, round() rounds the payable price to two decimal places, and number_format() displays the amounts in a readable format.
Concepts Covered
- Percentage calculation
- Discount calculation
round()functionnumber_format()- Real-world price calculations
Key Takeaways
abs()returns the positive value of a number.sqrt()calculates the square root.pow()calculates the power of a number.ceil()rounds a number upward.floor()rounds a number downward.round()rounds a number to the nearest value.max()returns the highest value.min()returns the lowest value.pi()returns the mathematical value of π.- Mathematical functions are useful for salary calculations, product pricing, discounts, and business reports.
Frequently Asked Questions
1. What are mathematical functions in PHP?
Mathematical functions are built-in PHP functions used to perform calculations such as rounding, finding square roots, calculating powers, and comparing numbers.
2. What is the use of abs() Function in PHP?
The abs() function returns the absolute value of a number. For example, abs(-50) returns 50.
3. What does sqrt() do in PHP?
The sqrt() function calculates the square root of a number. For example, sqrt(81) returns 9.
4. What is the difference between ceil() and floor()?
ceil() rounds a number upward, while floor() rounds it downward to the nearest integer.
5. How does round() work in PHP?
The round() function rounds a number to the nearest value. You can also specify the number of decimal places.
6. What is the use of pow() in PHP?
The pow() function calculates the power of a number. For example, pow(2, 4) returns 16.
7. What is the difference between max() and min()?
max() returns the largest value, while min() returns the smallest value from the supplied values.
8. How can I calculate the value of PI in PHP?
Use the pi() function. It returns an approximation of the mathematical constant π.
9. Can PHP mathematical functions work with decimal numbers?
Yes. Functions such as round(), ceil(), floor(), and abs() can work with decimal values.
10. Where are mathematical functions used in real PHP projects?
They are used in shopping websites, billing systems, salary calculations, discount calculations, financial reports, and other applications that require numerical calculations.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
