PHP has various types of numbers like Integer, Float, Double, etc. In addition, PHP provides automatic type conversion of variables. We can assign an integer value, a string to a variable, and the type of that variable will change to integer and string.
There are many types of numeric values. In this unit, we will discuss integers and floats in PHP, as well as the functions which can be used to determine the type of numbers that we are dealing with and convert between them. We will also learn how to convert integers and floats to and from numerical strings.
Read Also: PHP Strings
The most basic number type in PHP is the integer. Integers are numbers without any decimal part. For example, 2 is an integer.
One important thing is a number doesn’t need to be of type int if it does not have a decimal part. For example, 16*2.5 is 40, but the type of this result will still be a float.
We write the is_int($value) function to check if the number is of type integer. There are two more aliases of this function, called is_integer($value) and is_long($value). Both will give the same result.
<?php $x = 5985; var_dump(is_int($x)); $x = 59.85; var_dump(is_int($x)); ?>
The next most common type of number is a float. We can represent a number type float in a variety of ways. The values 3.14, 12.0, are float values.
PHP will convert a number to the float type whenever decimals or very large numbers are involved.
There are two functions which determines if the value is float or not. The functions are is_float() and is_double(). The is_double() is just an alias of is_float(), so you can use any one of them and get the same result.
<?php $x = 10.365; var_dump(is_float($x)); ?>
PHP infinity is different from infinity in real life. In PHP, the numerical value above approximately PHP_FLOAT_MAX is considered infinity.
We can check if a numerical value is finite or infinite using finite() and infinite() functions.
<?php $x = 1.9e411; var_dump($x); ?>
NaN stands for Not a Number.
NaN doesn’t check if a value is numerical or not. The value NaN is useful for the result of mathematical operations, which are not possible in mathematics. For example, log(-1) will be NaN.
<?php $x = acos(8); var_dump($x); ?>
PHP Numerical Strings
The function is_numeric() is useful to determine if a string or variable is numeric or not. The function will return true for numbers written in octal, binary, or hexadecimal notation. It will also return true if the numbers are written in exponential notation like +16.52e39 false otherwise.
<?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)); ?>
bool(true) bool(true) bool(true) bool(false)
PHP casting strings and floats to integers
We need to cast a numerical value into another data type. We can use (init) or (integer) to convert any value to an integer. In the case of floats, the values will always be rounded towards zero. Another way to cast strings and floats to integers is with the help of the intval() function. Both (int) and intval() work in the same manner.
<?php $x = 23465.768; $int_cast = (int)$x; echo $int_cast; $x = "23465.768"; $int_cast = (int)$x; echo $int_cast; ?>
Read More: PHP Math
One should be able to determine the type of a number or the final type of a result after using a variety of operations with predefined functions and also explicitly cast them to a specific type after doing some checks.
I hope you have a better understanding of PHP Numbers.