PHP Math
Mathematical operations and functions
➗ What is PHP Math?
PHP provides built-in mathematical functions for calculations including basic arithmetic, rounding, powers, roots, trigonometry, and random numbers. These functions make complex calculations simple and efficient.
<?php
$sum = 10 + 5; // Addition
$product = 10 * 5; // Multiplication
$power = pow(2, 3); // 2 to the power of 3
echo "Sum: $sum<br>";
echo "Product: $product<br>";
echo "Power: $power";
?>
Output:
Sum: 15
Product: 50
Power: 8
Math Functions
Basic Operations
Addition, subtraction, multiplication, and division operators for fundamental arithmetic calculations in your programs.
<?php
$sum = 10 + 5; // 15
$diff = 10 - 5; // 5
$prod = 10 * 5; // 50
?>
Rounding
Round numbers up, down, or to nearest integer using ceil(), floor(), and round() functions for precision control.
<?php
echo round(3.6); // 4
echo ceil(3.1); // 4
echo floor(3.9); // 3
?>
Power & Root
Calculate powers and square roots using pow() and sqrt() functions for exponential and radical operations.
<?php
echo pow(2, 3); // 8
echo sqrt(16); // 4
echo sqrt(25); // 5
?>
Random
Generate random numbers using rand() and mt_rand() for games, simulations, and unpredictable values.
<?php
echo rand(1, 10);
echo mt_rand(1, 100);
?>
Min & Max
Find minimum and maximum values from multiple numbers using min() and max() functions for comparisons.
<?php
echo min(1, 5, 3); // 1
echo max(1, 5, 3); // 5
?>
Absolute
Get absolute value of numbers using abs() function, converting negative numbers to positive equivalents.
<?php
echo abs(-5); // 5
echo abs(5); // 5
?>
🔹 Basic Arithmetic
Perform basic mathematical operations:
<?php
$a = 20;
$b = 5;
// Addition
$sum = $a + $b;
echo "Addition: $a + $b = $sum<br>";
// Subtraction
$diff = $a - $b;
echo "Subtraction: $a - $b = $diff<br>";
// Multiplication
$product = $a * $b;
echo "Multiplication: $a * $b = $product<br>";
// Division
$quotient = $a / $b;
echo "Division: $a / $b = $quotient<br>";
// Modulus (remainder)
$remainder = $a % $b;
echo "Modulus: $a % $b = $remainder";
?>
Output:
Addition: 20 + 5 = 25
Subtraction: 20 - 5 = 15
Multiplication: 20 * 5 = 100
Division: 20 / 5 = 4
Modulus: 20 % 5 = 0
🔹 Rounding Functions
Round numbers in different ways:
<?php
$number = 3.6;
// Round to nearest integer
echo "round($number) = " . round($number) . "<br>";
// Round up
echo "ceil($number) = " . ceil($number) . "<br>";
// Round down
echo "floor($number) = " . floor($number) . "<br>";
// Round to 2 decimals
$pi = 3.14159;
echo "round($pi, 2) = " . round($pi, 2) . "<br>";
// Round negative numbers
$negative = -3.6;
echo "round($negative) = " . round($negative);
?>
Output:
round(3.6) = 4
ceil(3.6) = 4
floor(3.6) = 3
round(3.14159, 2) = 3.14
round(-3.6) = -4
🔹 Power and Square Root
Calculate powers and roots:
<?php
// Power function
echo "pow(2, 3) = " . pow(2, 3) . "<br>";
echo "pow(5, 2) = " . pow(5, 2) . "<br>";
echo "pow(10, 0) = " . pow(10, 0) . "<br>";
// Square root
echo "sqrt(16) = " . sqrt(16) . "<br>";
echo "sqrt(25) = " . sqrt(25) . "<br>";
echo "sqrt(2) = " . round(sqrt(2), 2) . "<br>";
// Cube root (using pow)
echo "Cube root of 27 = " . pow(27, 1/3);
?>
Output:
pow(2, 3) = 8
pow(5, 2) = 25
pow(10, 0) = 1
sqrt(16) = 4
sqrt(25) = 5
sqrt(2) = 1.41
Cube root of 27 = 3
🔹 Min, Max, and Absolute
Find minimum, maximum, and absolute values:
<?php
// Minimum value
echo "min(5, 10, 3, 8) = " . min(5, 10, 3, 8) . "<br>";
echo "min(1.5, 2.3, 0.8) = " . min(1.5, 2.3, 0.8) . "<br>";
// Maximum value
echo "max(5, 10, 3, 8) = " . max(5, 10, 3, 8) . "<br>";
echo "max(1.5, 2.3, 0.8) = " . max(1.5, 2.3, 0.8) . "<br>";
// Absolute value
echo "abs(-10) = " . abs(-10) . "<br>";
echo "abs(10) = " . abs(10) . "<br>";
echo "abs(-3.5) = " . abs(-3.5);
?>
Output:
min(5, 10, 3, 8) = 3
min(1.5, 2.3, 0.8) = 0.8
max(5, 10, 3, 8) = 10
max(1.5, 2.3, 0.8) = 2.3
abs(-10) = 10
abs(10) = 10
abs(-3.5) = 3.5
🔹 Random Numbers
Generate random numbers:
<?php
// Random integer between 1 and 10
$random1 = rand(1, 10);
echo "Random (1-10): $random1<br>";
// Random integer between 1 and 100
$random2 = rand(1, 100);
echo "Random (1-100): $random2<br>";
// Better random with mt_rand
$random3 = mt_rand(1, 10);
echo "MT Random (1-10): $random3<br>";
// Random float between 0 and 1
$randomFloat = mt_rand() / mt_getrandmax();
echo "Random float: " . round($randomFloat, 4) . "<br>";
// Random dice roll
$dice = rand(1, 6);
echo "Dice roll: $dice";
?>
Output:
Random (1-10): 7
Random (1-100): 42
MT Random (1-10): 3
Random float: 0.6543
Dice roll: 5
🔹 Math Constants
PHP provides useful mathematical constants:
<?php
// Pi constant
echo "PI = " . M_PI . "<br>";
echo "Pi rounded = " . round(M_PI, 2) . "<br>";
// Euler's number
echo "E = " . M_E . "<br>";
echo "E rounded = " . round(M_E, 2) . "<br>";
// Calculate circle area
$radius = 5;
$area = M_PI * pow($radius, 2);
echo "Circle area (r=$radius) = " . round($area, 2) . "<br>";
// Calculate circle circumference
$circumference = 2 * M_PI * $radius;
echo "Circle circumference (r=$radius) = " . round($circumference, 2);
?>
Output:
PI = 3.14159265359
Pi rounded = 3.14
E = 2.71828182846
E rounded = 2.72
Circle area (r=5) = 78.54
Circle circumference (r=5) = 31.42