Tutorials › PHP Tutorial › PHP Numbers and Math

PHP Numbers and Math

12 min read · Lesson 8 of 24

PHP has two number types: integers (whole numbers) and floats (numbers with decimals). This lesson shows how to check them, convert them, and use PHP's built-in math functions for everyday tasks such as rounding prices or picking random numbers.

Integers and floats

Example PHP
<?php
$students = 35;        // int
$average = 72.4;       // float
$big = 2.5e6;          // float in exponent form

var_dump($students, $average, $big);
var_dump(10 / 4);      // division can produce a float
var_dump(10 / 5);      // still a float... or is it?
Result
int(35)
float(72.4)
float(2500000)
float(2.5)
int(2)
Open in editor

When / divides evenly between two integers, the result is an int; otherwise it is a float. For whole-number division that always gives an integer, use intdiv().

Checking number types

FunctionReturns true when...
is_int($x)$x is an integer
is_float($x)$x is a float
is_numeric($x)$x is a number or a numeric string such as "45" or "4.5"
is_nan($x)$x is "not a number" (for example sqrt(-1))
Example PHP
<?php
var_dump(is_int(5));
var_dump(is_float(5));
var_dump(is_numeric("2500"));
var_dump(is_numeric("25 bob"));
Result
bool(true)
bool(false)
bool(true)
bool(false)
Open in editor

is_numeric() is very useful for checking form input, because everything sent from a form arrives as a string.

Converting to numbers

Example PHP
<?php
$fromForm = "1500";
$price = (int) $fromForm;
var_dump($price);
var_dump((float) "12.75");
var_dump(intval("08"));
var_dump(floatval("3.9kg"));
Result
int(1500)
float(12.75)
int(8)
float(3.9)
Open in editor

Limits: infinity and NaN

Integers have a maximum size (PHP_INT_MAX). If a calculation goes beyond it, PHP switches to a float. Floats that grow too big become INF (infinity).

Example PHP
<?php
var_dump(PHP_INT_MAX + 1);
var_dump(1.8e308);
var_dump(is_nan(sqrt(-1)));
Result
float(9.223372036854776E+18)
float(INF)
bool(true)
Open in editor
Note: Dividing by zero with / or % throws a DivisionByZeroError in PHP 8, which stops your script unless you catch it. Always check a divisor that comes from user input.

Math functions

Rounding

FunctionWhat it doesx = 4.567
round($x)nearest whole number5
round($x, 2)nearest with 2 decimals4.57
floor($x)always down4
ceil($x)always up5
Example PHP
<?php
$bill = 1234.567;
echo round($bill) . "\n";
echo round($bill, 2) . "\n";
echo floor($bill) . "\n";
echo ceil($bill) . "\n";

// How many 14-seater matatus for 40 passengers?
echo ceil(40 / 14) . " matatus\n";
Result
1235
1234.57
1234
1235
3 matatus
Open in editor

Min, max, absolute value and powers

Example PHP
<?php
$marks = [67, 82, 45, 90, 73];
echo max($marks) . "\n";
echo min($marks) . "\n";
echo min(12, 7, 30) . "\n";
echo abs(-250) . "\n";
echo sqrt(81) . "\n";
echo pow(2, 10) . "\n";
echo 2 ** 3 . "\n";
echo intdiv(17, 5) . " remainder " . 17 % 5 . "\n";
echo pi() . "\n";
Result
90
45
7
250
9
1024
8
3 remainder 2
3.1415926535898
Open in editor

Random numbers

random_int($min, $max) returns a random whole number, including both ends. It is cryptographically secure, so it is safe for things like one-time PIN codes. (rand() and mt_rand() also exist, but are not safe for secrets.)

<?php
$otp = random_int(100000, 999999);
echo "Your verification code is $otp";
// e.g. Your verification code is 482915  (different every time)

Formatting numbers for display

number_format() adds thousands separators and fixes the number of decimals, perfect for prices.

Example PHP
<?php
$revenue = 1250000.5;
echo number_format($revenue) . "\n";
echo number_format($revenue, 2) . "\n";
echo "KSh " . number_format(3499.999, 2) . "\n";
echo number_format(1234.5, 2, ",", ".") . "\n";
Result
1,250,001
1,250,000.50
KSh 3,500.00
1.234,50
Open in editor

The last line uses a comma for decimals and a dot for thousands, the style used in many European countries.

Tip: Do your calculations with raw numbers and only call number_format() at the very end, when printing. The result of number_format() is a string with commas, so you cannot do math with it any more.
Exercise

Round 7.256 to two decimal places.

echo (7.256, 2);
Exercise

Always round 4.1 up to the next whole number.

echo (4.1);
Exercise

Print 15000 as 15,000.

echo (15000);
Sign in or create a free account to track progress, earn XP, badges and a certificate.