Numbers in PHP
Master numeric operations in PHP: integers, floats, math functions, rounding, random numbers, powers, roots, number formatting, and type conversions.
Creating Numbers
PHP has two numeric types: `int` (whole numbers) and `float` (decimal numbers). Both support various literal notations and PHP provides constants for their limits.
1. Integers
Whole numbers — positive, negative, or zero. Can be written in decimal, hex, octal, binary, or with underscore separators (PHP 7.4+).
2. Floats
Decimal and scientific-notation numbers. Watch out for floating-point precision.
Finding Min and Max
PHP provides `min()` and `max()` that work with both individual arguments and arrays.
1. min() and max()
Pass values directly or pass an array — both forms are supported.
Rounding Numbers
PHP offers four rounding functions: `round()`, `floor()`, `ceil()`, and `intval()` — each with a different rounding strategy.
1. round() — Round to Nearest
`round($n, $precision)` rounds to the nearest value. Default precision is 0 (integer).
2. floor() and ceil()
`floor()` always rounds down; `ceil()` always rounds up — regardless of the decimal.
3. Rounding to N Decimal Places
Use `round($n, $decimals)` or `number_format()` for display-ready output.
Random Numbers
PHP provides `rand()` / `mt_rand()` for general random numbers and `random_int()` for cryptographically secure integers (PHP 7.0+).
1. rand() and mt_rand()
`rand($min, $max)` generates a random integer. `mt_rand()` is faster and uses a better algorithm.
2. random_int() — Cryptographically Secure (PHP 7.0+)
Use `random_int()` whenever the random value is used for security (tokens, OTPs, passwords).
Powers and Roots
PHP supports the `**` exponentiation operator (PHP 5.6+) and provides `sqrt()`, `pow()`, and `log()` for common math operations.
1. Power — ** and pow()
Both `**` and `pow()` raise a number to a given exponent.
2. sqrt() and Nth Root
`sqrt()` finds the square root. Use `pow($n, 1/$k)` for the kth root.
Common Math Functions
PHP ships with a comprehensive math library. These are the functions you will reach for most often in everyday development.
1. abs(), fmod(), and intdiv()
`abs()` for absolute value, `fmod()` for float modulo, `intdiv()` for integer division (PHP 7.0+).
2. log(), log10(), log2()
Logarithm functions for different bases.
3. PHP Math Constants
PHP ships with pre-defined constants for common mathematical values.
Validating Numbers
PHP provides a family of functions for numeric validation — essential when handling user input from forms or APIs.
1. is_numeric()
`is_numeric()` returns `true` for both actual numbers and numeric strings.
2. is_int(), is_float(), is_finite(), is_nan()
Type and value checks for numeric variables.
Number ↔ String Conversion
Converting between numbers and strings is a daily task in PHP — handling form inputs, building output, and formatting data.
1. Number → String
Use `(string)` cast, `strval()`, or `number_format()` to convert a number to a string.
2. String → Number
Use `(int)`, `(float)`, `intval()`, or `floatval()` — always validate with `is_numeric()` first.