9. PHP Numbers and Math
What you will learn
- Integers and floats
- Rounding and formatting
- Useful math functions
- Random numbers, and checking numeric input
Integers and floats
An int is a whole number and a float has a decimal part. PHP chooses the type from the value.
PHP code
<?php
var_dump(7);
var_dump(7.0);
var_dump(7 / 2);
var_dump(7 % 2);
var_dump(2 ** 3);
var_dump(intdiv(7, 2));
What you should see (inside <pre>)
int(7) float(7) float(3.5) int(1) int(8) int(3)
% gives the remainder, ** is the power, and intdiv() divides and drops the remainder.
Rounding
| Function | What it does |
|---|---|
round($x, $places) |
Nearest value, optionally to decimal places |
ceil($x) |
Round up |
floor($x) |
Round down |
PHP code
<?php
echo round(4.6) . "<br>";
echo round(3.14159, 2) . "<br>";
echo ceil(4.1) . "<br>";
echo floor(4.9);
What the browser shows
5
3.14
5
4
More math functions
PHP code
<?php
echo abs(-7) . "<br>";
echo max(3, 9, 5) . "<br>";
echo min(3, 9, 5) . "<br>";
echo sqrt(81) . "<br>";
echo pow(2, 10) . "<br>";
echo round(pi(), 4);
What the browser shows
7
9
3
9
1024
3.1416
Formatting for display
number_format() adds thousands separators and fixes decimals. It returns text, so use it for showing numbers, not for calculating.
PHP code
<?php
$price = 1234567.891;
echo number_format($price) . "<br>";
echo number_format($price, 2) . "<br>";
echo number_format($price, 2, ",", ".");
What the browser shows
1,234,568
1,234,567.89
1.234.567,89
Random numbers
random_int($min, $max) gives a secure random whole number, both ends included. rand() is older and not secure. Use random_int() for anything that matters, such as tokens.
PHP code
<?php
echo "You rolled: " . random_int(1, 6);
Output (a different number each time)
You rolled: 4
Float accuracy
Computers store decimals in binary, so some sums are very slightly off, in every language.
PHP code
<?php
var_dump(0.1 + 0.2 == 0.3);
var_dump(abs((0.1 + 0.2) - 0.3) < 0.00001);
What you should see (inside <pre>)
bool(false) bool(true)
Compare floats with a small tolerance. For money, many developers store whole cents as integers.
Checking numeric input
Form values are strings. Check them before calculating.
PHP code
<?php
var_dump(is_numeric("42"));
var_dump(is_numeric("4.2e1"));
var_dump(is_numeric("42abc"));
var_dump(is_numeric(""));
var_dump(filter_var("42", FILTER_VALIDATE_INT));
var_dump(filter_var("4.5", FILTER_VALIDATE_INT));
What you should see (inside <pre>)
bool(true) bool(true) bool(false) bool(false) int(42) bool(false)
filter_var() with FILTER_VALIDATE_INT returns the integer, or false when the text is not a whole number.
Common mistakes
- Comparing floats with
==. - Using
number_format()output in further calculations. - Using
rand()for security. - Dividing by zero, which throws an error in PHP 8.
- Trusting form input to be numeric.
Exercise
A price is 19.99 and the tax rate is 0.2. Print the price including tax, rounded to 2 decimals.
Show solution
<?php
$price = 19.99;
$tax_rate = 0.2;
$total = $price * (1 + $tax_rate);
echo number_format($total, 2);
The output is 23.99.
Knowledge check
1. Which function gives a secure random integer?
random_int()
2. What does intdiv(7, 2) return?
3.
3. Why not compare floats with ==?
Binary storage makes some results slightly inexact.
Summary
- PHP has int and float, with
round,ceil,floor,abs,max, andmin. number_format()is for display only.- Use
random_int()and validate numeric input.