Skip to content
Numbers and Math
step 1/5

Reading — step 1 of 5

Learn

~1 min readGetting Started

Standard arithmetic: + - * / %. PHP's / returns a float when the result isn't a whole number — 7 / 3 is 2.333.... For integer division use intdiv($a, $b).

$a = 17;
$b = 5;
echo $a + $b, "\n";          // 22
echo $a / $b, "\n";          // 3.4 (float)
echo intdiv($a, $b), "\n";    // 3
echo $a % $b, "\n";          // 2
echo $a ** $b, "\n";         // 1419857 (power)

Useful number functions: abs, floor, ceil, round, min, max, pow, sqrt. intval($s) and floatval($s) parse strings safely.

Integers are automatically promoted to floats on overflow. PHP's max int is PHP_INT_MAX (9.2 × 10^18 on 64-bit).

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…

Numbers and Math — PHP Fundamentals