Integers and Floats

Integers are whole numbers; floats are numbers with a decimal point or in exponential form.

An integer is a number without a decimal point, for example -5, 0 or 100.

A float (or double) is a number with a decimal point such as 3.14, or one in exponential form like 2.5e3.

You can test the type with is_int() and is_float().

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Store a product's stock count as an integer and its price as a float in a shopping cart.
  • Use PHP_INT_MAX to guard against overflow before adding large counters together.
  • Round a calculated tax amount to two decimal places before displaying it to a user.

More examples

Integer limits and literals

PHP integers support hex and octal literals, and underscores improve readability for large numbers.

Example · php
<?php
$stock = 1_000_000;  // underscores for readability
$hex   = 0x1A;       // 26 in decimal
$octal = 0755;       // Unix file permission

echo PHP_INT_MAX;    // 9223372036854775807 on 64-bit

Float arithmetic and precision

Floating-point arithmetic is inexact; round() and number_format() produce clean output for display.

Example · php
<?php
$price = 1.1 + 2.2;
echo $price;                   // 3.3000000000000004 (float issue)
echo round($price, 2);         // 3.3
echo number_format($price, 2); // 3.30

Checking integer and float types

is_int(), is_float(), and is_numeric() validate numeric inputs before performing calculations.

Example · php
<?php
$qty   = 5;
$price = 19.99;

var_dump(is_int($qty));        // bool(true)
var_dump(is_float($price));    // bool(true)
var_dump(is_numeric("3.14")); // bool(true)

Discussion

  • Be the first to comment on this lesson.