PHP has various types of numbers like Integer, Float, Double, etc. Also, 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 whole numbers that can be positive, negative or zero. In Computer Science, they are generally defined as a group of binary bits. Few examples of Integers are 2, -45, 2346, etc.
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 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)); echo "<br>"; $x = 59.85; var_dump(is_int($x)); ?>
The next most common type of number is float. We can represent a number type of 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.
Two functions determine 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 defined 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 in determining 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, and false otherwise.
<?php $x = 5985; var_dump(is_numeric($x)); echo "<br>"; $x = "5985"; var_dump(is_numeric($x)); echo "<br>"; $x = "59.85" + 100; var_dump(is_numeric($x)); echo "<br>"; $x = "Hello"; var_dump(is_numeric($x)); ?>
bool(true) bool(true) bool(true) bool(false)
PHP casting strings and floats to integers
We can also 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,"<br>"; $x = "23465.768"; $int_cast = (int)$x; echo $int_cast,"<br>"; ?>
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. Understand the predefined functions and also cast them to a specific type after doing some checks.
We hope you have gained a clear understanding of the different types of numbers used in PHP programming. We suggest you to apply them in your initial programs and observe the varying outputs. Refer to our previous blogs in case you want to refresh some of your other basics. Stay motivated and keep learning!