PHP Numbers
PHP numbers come in three main forms:
IntegerFloatNumeric Strings
You will also run into two special numeric values:
InfinityNaN
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:
Output: PHP script
<?php
$a = 5;
$b = 5.34;
$c = "25";
var_dump($a);
var_dump($b);
var_dump($c);
?>
Numeric variable types
Each of these variables gets its type from the value assigned to it:
<?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 availablePHP_INT_MIN- the lowest integer availablePHP_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:
Output: PHP script
<?php
$x = 5985;
var_dump(is_int($x));
$y = 59.85;
var_dump(is_int($y));
?>
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 representPHP_FLOAT_MIN- the smallest positive float PHP can representPHP_FLOAT_DIG- how many decimal digits survive being rounded into a float and back with nothing lostPHP_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:
Output: PHP script
<?php
$x = 10.365;
var_dump(is_float($x));
?>
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:
Output: PHP script
<?php
$x = 1.9e411;
var_dump(is_infinite($x));
?>
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:
Output: PHP script
<?php
$x = acos(8);
var_dump($x);
// Check if value is not a number (NaN)
var_dump(is_nan($x));
?>
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:
Output: PHP script
<?php
$x = 5985;
var_dump(is_numeric($x));
$x = "5985";
var_dump(is_numeric($x));
$x = "59.85" + 100;
var_dump(is_numeric($x));
$x = "Hello";
var_dump(is_numeric($x));
?>
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:
Output: PHP script
<?php
$x = 23465.768;
echo intval($x);
echo "\n";
$x = "23465.768";
echo intval($x);
?>