Home PHP Numbers PHP Numbers Definition
Definition and Usage PHP has two numeric types: integers (whole numbers such as 42) and floats (decimal numbers such as 3.14). You combine them with arithmetic operators and convert other values with casting .
Math helpers such as abs() , round() , sqrt() , and max() are global functions . They are documented on the Math Functions index .
/ may return a float. 17 / 5 is 3.4. Use intdiv(17, 5) for the whole-number result 3, and % for the remainder.
Floats are approximate. 0.1 + 0.2 is not exactly 0.3 in binary floating point. Round for display and compare with a tolerance.
Syntax
PHP Syntax Literals, operators, casts, and a function call:
$count = 42; // int
$price = 19.99; // float
$sum = $count + $price; // + - * / % **
$whole = (int) $price; // cast: 19
$rate = (float) "3.5"; // cast: 3.5
$root = sqrt(16); // math helper: 4 Code
PHP Examples Four short scripts covering the number basics.
1. Types 2. Arithmetic 3. Casting 4. Helpers Example 1 — Integers and floats Check which numeric type a value has.
<?php
$count = 42;
$price = 19.99;
var_dump($count);
var_dump($price);
echo gettype($count) . ' / ' . gettype($price) . "\n";
?> int(42)
float(19.99)
integer / double How It Works 42 has no decimal point, so it is an int.19.99 has a decimal point, so it is a float.gettype() reports floats as double for historical reasons.Example 2 — Basic arithmetic Use the core operators on two integers.
<?php
$a = 17;
$b = 5;
echo 'add: ' . ($a + $b) . "\n";
echo 'subtract: ' . ($a - $b) . "\n";
echo 'multiply: ' . ($a * $b) . "\n";
echo 'divide: ' . ($a / $b) . "\n";
echo 'remainder: ' . ($a % $b) . "\n";
echo 'power: ' . ($a ** 2) . "\n";
?> add: 22
subtract: 12
multiply: 85
divide: 3.4
remainder: 2
power: 289 How It Works + - * behave as in school math./ returns a float when the division is not exact.% gives the remainder and ** raises to a power.Example 3 — Casting to int and float Convert strings and floats into the type you need.
<?php
echo (int) 3.99 . "\n";
echo (int) '42abc' . "\n";
echo (int) 'abc' . "\n";
echo (float) '3.14' . "\n";
?> How It Works (int) 3.99 truncates toward zero—it does not round.(int) '42abc' reads the leading digits and stops at a.(int) 'abc' has no leading number, so the result is 0.Example 4 — Math helper functions Call a few helpers from the Math Functions index.
<?php
echo 'abs: ' . abs(-7.5) . "\n";
echo 'round: ' . round(3.14159, 2) . "\n";
echo 'sqrt: ' . sqrt(16) . "\n";
echo 'max: ' . max(3, 9, 5) . "\n";
?> abs: 7.5
round: 3.14
sqrt: 4
max: 9 How It Works abs() removes the minus sign.round() rounds to the given number of decimals.sqrt() returns a float; max() picks the largest argument.Continue
Next: PHP Math Functions Index Once you know ints, floats, and operators, open the index for every math helper in this series.
Start with Why abs()Absolute value (drop the sign) round()Round to a chosen precision sqrt()Square root max()Largest of several values
Full list: PHP Math Functions index (/php/math/function).
Frequently Asked Questions What number types does PHP have? PHP has two numeric types: int for whole numbers (42, -7) and float for decimals (3.14, 1.5e3). Use var_dump() or gettype() to see which type a value has.
What is the difference between / and intdiv()? The / operator returns an int only when the division is exact; otherwise it returns a float (17 / 5 is 3.4). intdiv() always returns the whole-number quotient (intdiv(17, 5) is 3) and throws DivisionByZeroError if the divisor is 0.
How do I convert a value to a number? Cast with (int) or (float), or use intval() and floatval(). Casting a string reads the leading numeric part: (int) "42abc" is 42, and (int) "abc" is 0. Casting a float to int truncates toward zero.
Why does 0.1 + 0.2 not equal 0.3? Floats are stored in binary, so some decimals cannot be represented exactly. Compare floats with a small tolerance (abs($a - $b) < 0.00001) instead of ==, and use round() when displaying results.
Where are abs(), round(), sqrt(), and max() documented? Math helpers are global functions. They are listed on the PHP Math Functions index, grouped by purpose, with a full guide for each function.
Browse every math function Open the index for categorized math guides with examples and FAQs.
Math Functions Index → About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful
Helpful Share Copy link Suggestion