Skip to main content

PHP Numbers

PHP numbers come in three main forms:

  • Integer
  • Float
  • Numeric Strings

You will also run into two special numeric values:

  • Infinity
  • NaN

PHP creates a numeric variable as soon as you assign a number to it, with no separate declaration needed.

Whenever you're unsure what type a variable or object has, pass it to var_dump().

Verify data types

var_dump() reports both type and value, so you can see that $c is a string even though it holds digits:

Prompt
Write a PHP script that creates three variables: $a as integer 5, $b as float 5.34, and $c as string "25", then uses var_dump() on each to display their data types.
Output: PHP script

Numeric variable types

Each of these variables gets its type from the value assigned to it:

index.php
<?php
$a = 5;
$b = 5.34;
$c = "25";
?>

The PHP is_int() Function

To test whether a variable holds an integer, use is_int().

Integers are whole numbers with no decimal part, such as 2, 256, -256, 10358 and -179567.

Their range depends on the system. On 32-bit systems an integer runs from -2147483648 to 2147483647, and on 64-bit systems from -9223372036854775808 to 9223372036854775807. Go past either limit and PHP stores the value as a float instead.

Note: A calculation involving a float also gives a float. For example, 4 * 2.5 equals 10, but that 10 is stored as a float because 2.5 is one of the operands.

Keep these rules in mind when writing integers:

  • At least one digit is required
  • A decimal point is not allowed
  • The value may be positive or negative
  • Four notations are available: decimal (base 10), hexadecimal (base 16, with the prefix 0x), octal (base 8, with the prefix 0) and binary (base 2, with the prefix 0b)

PHP also gives you three built-in constants for integers:

  • PHP_INT_MAX - the biggest integer available
  • PHP_INT_MIN - the lowest integer available
  • PHP_INT_SIZE - how many bytes an integer takes up

Check if variable is integer

$x holds a whole number and $y does not, so only the first check gives true:

Prompt
Write a PHP script that checks if the integer 5985 is an integer using is_int() and var_dump(), then checks if the float 59.85 is an integer.
Output: PHP script

The PHP is_float() Function

is_float() tells you whether a variable holds a float.

Floats are numbers written with a decimal point or in exponential form, for example 2.0, 256.4, 10.358, 7.64E+5 and 5.56E-5.

How large a float can be depends on the platform, but a common ceiling is about 1.7976931348623E+308, with up to 14 digits of precision.

Since PHP 7.2, these built-in constants describe floats:

  • PHP_FLOAT_MAX - the biggest float PHP can represent
  • PHP_FLOAT_MIN - the smallest positive float PHP can represent
  • PHP_FLOAT_DIG - how many decimal digits survive being rounded into a float and back with nothing lost
  • PHP_FLOAT_EPSILON - the smallest positive x for which x + 1.0 != 1.0

Check if variable is float

The value 10.365 has a decimal point, so the check gives true:

Prompt
Write a PHP script that checks if the value 10.365 is a float using is_float() and var_dump().
Output: PHP script

PHP Infinity

A PHP float has an allowed range on each platform. A value inside that range counts as finite, and is_finite() lets you test for it.

A value beyond that range counts as infinite, which is what is_infinite() checks.

Check if a value is infinite

1.9e411 is too large for a float, so PHP treats it as infinite:

Prompt
Write a PHP script that checks if the value 1.9e411 is infinite using is_infinite() and var_dump().
Output: PHP script

PHP NAN and is_nan() Function

The name NAN is short for "Not A Number". It is what you get from a mathematical operation that makes no sense.

To detect this result, pass the value to is_nan().

Check for NAN value

The arc cosine of 8 isn't defined, so acos(8) produces NAN, and is_nan() confirms it:

Prompt
Write a PHP script that calculates acos(8), dumps the result, then checks if it is NaN using is_nan() and var_dump().
Output: PHP script

PHP Number and Numerical Strings

Numbers aren't always stored as numbers; text such as "5985" is a numeric string. is_numeric() gives true for both of these cases, and false for anything else.

Check if variable is numeric

Only "Hello" fails the test here; the number, the numeric string and the result of the addition all pass:

Prompt
Write a PHP script that checks if 5985, "5985", "59.85" + 100, and "Hello" are numeric using is_numeric() and var_dump().
Output: PHP script

PHP intval(): Get the Integer Value of a Variable

When you need a whole number out of a variable, pass it to intval().

Get integer value

A float and a string both give 23465, because the decimal part is dropped:

Prompt
Write a PHP script that gets the integer value of float 23465.768 and string "23465.768" using intval() and echo.
Output: PHP script