PHP Numbers

Beginner
6 min read
Updated: Nov 2026
4 examples
int & float types

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.

PHP Syntax

Literals, operators, casts, and a function call:

PHP
$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

PHP Examples

Four short scripts covering the number basics.

Example 1 — Integers and floats

Check which numeric type a value has.

PHP
<?php
$count = 42;
$price = 19.99;

var_dump($count);
var_dump($price);
echo gettype($count) . ' / ' . gettype($price) . "\n";
?>

How It Works

  1. 42 has no decimal point, so it is an int.
  2. 19.99 has a decimal point, so it is a float.
  3. gettype() reports floats as double for historical reasons.

Example 2 — Basic arithmetic

Use the core operators on two integers.

PHP
<?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";
?>

How It Works

  1. + - * behave as in school math.
  2. / returns a float when the division is not exact.
  3. % 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
<?php
echo (int) 3.99 . "\n";
echo (int) '42abc' . "\n";
echo (int) 'abc' . "\n";
echo (float) '3.14' . "\n";
?>

How It Works

  1. (int) 3.99 truncates toward zero—it does not round.
  2. (int) '42abc' reads the leading digits and stops at a.
  3. (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
<?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";
?>
round() Tutorial

How It Works

  1. abs() removes the minus sign.
  2. round() rounds to the given number of decimals.
  3. sqrt() returns a float; max() picks the largest argument.

Next: PHP Math Functions Index

Once you know ints, floats, and operators, open the index for every math helper in this series.

Start withWhy
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

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.
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.
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.
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.
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

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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