📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials PHP for Beginners Math Functions

Math Functions

4 min read Quiz at the end
PHP includes math functions like abs(), round(), ceil(), floor(), and rand(). Use operators like + - * / for basic arithmetic. These are useful for calculations in shopping carts, scores, and financial reports.

Math Functions

abs(-5);          // 5
ceil(4.1);        // 5  (round up)
floor(4.9);       // 4  (round down)
round(4.5);       // 5
round(3.14159, 2);// 3.14
sqrt(16);         // 4
pow(2, 8);        // 256
max(1, 5, 3);     // 5
min(1, 5, 3);     // 1
rand(1, 100);     // random between 1-100
mt_rand(1, 100);  // faster random
number_format(1234567.891, 2, ".", ","); // 1,234,567.89
intdiv(7, 2);     // 3  (integer division)
fmod(7, 2);       // 1.0 (float modulo)
log(M_E);         // 1 (natural log)
pi();             // 3.14159...
PHP_INT_MAX;      // 9223372036854775807
Topic Quiz · 5 questions

Test your understanding before moving on

1. Which function rounds up?
💡 ceil() always rounds a number up to the nearest integer.
2. What does rand(1, 10) return?
💡 rand() returns a random integer between min and max (inclusive).
3. What does intdiv(7, 2) return?
💡 intdiv() performs integer division, returning the quotient without remainder.
4. Which function formats a number with thousands separator?
💡 number_format(1234567.89, 2) returns "1,234,567.89".
5. What is M_PI in PHP?
💡 M_PI is a predefined PHP constant equal to pi (3.14159265...).