Numbers In PHP
Dive into the world of PHP numbers with this comprehensive guide. We will explore the various techniques and methods for handling numbers in PHP, complete with practical examples to help you understand the concepts better.
The world of programming revolves around numbers, and PHP is no exception. The PHP server side scripting language uses numbers for a variety of tasks such as mathematical computations, manipulation of data, and decision making.
We are going to look in depth at Integers, Floats, and Number Strings.
PHP Numbers
You can easily convert data types using PHP Numbers because it provides automatic data type conversions.
An integer value will automatically translate into an integer type if it is assigned to a variable.
If you then assign a string to the same variable, the type of the variable will change to a string.
In some cases, this automatic conversion can cause your code to break.
Integers In PHP
The following numbers are all integers:
2, 256, –256, 10358, -179567.
In PHP, an integer is a number that does not have a decimal part.
When we talk about Php Numbers, There are two types of integer data types: one is a non-decimal number between -2147483648 and 2147483647 in 32 bit systems, and the other is a non-decimal number between -9223372036854775808 and 9223372036854775807 in 64 bit systems.
It is likely that any value above (or below) this will be stored as a float, for the simple reason that it exceeds the integer limit.
Integer rules:
- One digit is required for an integer.
- No decimal point in an integer.
- Integers can be either positive or negative.
- Binary (base 2), octal (base 8), decimal (base 10), and hexadecimal (base 16) notation can be used to specify integers.
Here are PHP’s predefined integer constants:
- PHP_INT_MAX – Supports the largest integer.
- PHP_INT_MIN – Supports the smallest integer.
- PHP_INT_SIZE – The byte size of an integer.
In PHP, you can check a variable’s type by using these functions:
- is_int()
- is_integer() – alias of is_int()
- is_long() – alias of is_int()
Below is an example that demonstrates the use of the is_int() function:
Example: 
The following example checks whether a given variable is an integer using the is_int() function under the if_else statement:
Example: 
PHP Floats
The term float refers to a PHP number with a decimal point at the end or an exponential function.
2.0, 256.4, 10.358, 7.64E+5, 5.56E-5 are all floating point numbers.
There can be a maximum precision of 14 digits with the float data type.
This data type can store a value up to 1.7976931348623E+308 (platform dependent).
There are several predefined constants in PHP that are used for float values (from PHP 7.2):
- PHP_FLOAT_MAX – A floating point number that represents the largest possible value.
- PHP_FLOAT_MIN – A floating point PHP number that represents the smallest possible value.
- – PHP_FLOAT_MAX – A floating point number that represents the smallest negative value.
- PHP_FLOAT_DIG – Number of decimal digits that can be rounded to floats without losing precision.
- PHP_FLOAT_EPSILON – The smallest positive number a, so that a + 1.0 != 1.0.
In PHP Numbers, you can check whether variables are floats by using the following functions:
- is_float()
- is_double() – alias of is_float()
Below is an example that demonstrates the use of the is_float() function:
Example: 
In the below example we are checking whether PHP number is a float or integer:
Example: 
PHP Infinity
When a numeric value exceeds PHP_FLOAT_MAX, it is considered infinite.
Here are PHP functions to check if a number is finite or infinite:
- is_finite()
- is_infinite()
Data type and value are returned by the PHP var_dump() function
Below is an example that checks if a PHP number is finite or infinite:
Example: 
The following example shows finite and infinite values:
Example: 
PHP NaN
A NaN is a Not a Number.
For mathematical operations that are impossible, NaN is used.
If you want to check if a value is not a PHP number, then you can use the following function:
- is_nan()
Data type and value are returned by the PHP var_dump() function
Below is an example of using the acos() (Arc cos) function, If the calculation is invalid, a NaN value will be returned:
Example: 
Below is an example of using the asin() (Arc sine) function, If the calculation is invalid, a NaN value will be returned:
Example: 
PHP Numerical Strings
If we are talking about PHP numbers then, we can use the PHP is_numeric() function to determine whether a variable is a numeric value or not.
If the variable is either a number or a numeric string, the function will return true, otherwise, it will return false.
Below is an example that checks each $a, if it is numeric:
Example: 
Below is an example that checks each $b, if it is numeric:
Example: 
Remember: As of PHP 7.0, the is_numeric() function will return FALSE for numeric strings in hexadecimal form, e.g. 0xf4c3b00c, since hexadecimal strings are no longer considered numeric strings.
Cast Strings and Floats to Integers
A numerical value can sometimes be converted into another data type.
A value can be converted to an integer using (int), (integer), or intval().
In the below example, floats and strings are converted to integers using the following method:
Example: 
Below is an another example, that floats and strings are converted to integers using the following method:
Example: 
Example Explanation:
In above example we performs typecasting of a float and a string to an integer using the (int) operator.
- We have defined a variable $b and assigns it a value of 3874.6465, which is a floating-point PHP number.
- Then we perform typecasting by using the (int) operator, which converts the float value to an integer. The resulting value is stored in a variable called $cast_to_int.
- Then we have used echo statement to display the value of $cast_to_int on the screen. Since the original value was a float, the resulting value will be truncated to an integer, which is 3874.
- Then we have reassign the variable $b to a string with the same value, “3874.6465”.
- Again we perform typecasting using (int), this time on the string value of $b. The resulting value is stored in $cast_to_int.
- Finally, we have used the echo statement to display the value of $cast_to_int on the screen. Since the original value was a string, typecasting will attempt to convert the string to an integer, and will remove any non-numeric characters. In this case, the resulting value is 3874, which is the integer part of the original string.
Above example demonstrates how to perform typecasting of a float and a string to an integer in PHP using the (int) operator, and shows how the resulting value is affected by the original data type.