Home »
PHP
PHP Numbers, Floats, Infinity, NaN, and Numerical Strings
By Shahnail Khan Last updated : December 5, 2023
Numbers are the building blocks of programming, and understanding how PHP handles integers, floats, infinity, NaN, and numerical strings is crucial. Let's understand the PHP Numbers in detail.
An integer is a whole number without any decimal part. It can be positive or negative and is used for counting and ordering.
PHP Number/Integer
In PHP, an integer is a type of data that can store whole numbers. There are limits to how large or small these numbers can be in 32-bit and 64-bit systems.
- For 32-bit systems: Numbers must be between -2147483648 and 2147483647.
- For 64-bit systems: Numbers must be between -9223372036854775808 and 9223372036854775807.
If a number exceeds these limits, it's stored as a float (a number with decimal points).
Rules for Integers
- An integer must have at least one digit:
Example: 5, 123, -789
- It cannot have a decimal point:
Example: 10, -42, 789
- It can be positive or negative:
- Positive integers: 25, 1000, 9876
- Negative integers: -50, -789, -1234
- Integers can be written in three formats:
- Decimal (regular numbers):
Example: 456, 789, -23
- Hexadecimal (prefixed with 0x):
Example: 0x1A (which is 26 in decimal), 0xFF (which is 255 in decimal)
- Octal (prefixed with 0):
Example: 017 (which is 15 in decimal), 077 (which is 63 in decimal)
PHP Constants for Integers
PHP provides some built-in constants for integers:
1. PHP PHP_INT_MAX Constant
This constant represents the largest integer that PHP supports.
Example:
<?php
$maxInt = PHP_INT_MAX;
echo "The largest integer PHP supports is: $maxInt";
?>
The output of the above example is:
The largest integer PHP supports is: 9223372036854775807
Note: This might vary depending on your system.
2. PHP PHP_INT_MIN Constant
This constant represents the smallest integer that PHP supports.
<?php $minInt = PHP_INT_MIN;
echo "The smallest integer PHP supports is: $minInt";
?>
The output of the above example is:
The smallest integer PHP supports is: -9223372036854775808
Note: This might vary depending on your system.
3. PHP PHP_INT_SIZE Constant
This constant provides the size of an integer in bytes.
<?php $intSize = PHP_INT_SIZE;
echo "The size of an integer in bytes is: $intSize";
?>
The output of the above example is:
The size of an integer in bytes is: 8
Note: This might be 4 on 32-bit systems.
PHP Functions to Check Integer Type
PHP has three functions to check if a variable is of integer type:
1. PHP is_int() Function:
Checks if a variable is an integer.
Example:
<?php
$number = 800;
if (is_int($number)) {
echo "$number is an integer.";
} else {
echo "$number is not an integer.";
}
?>
The output of the above example is:
800 is an integer.
2. PHP is_integer() Function
An alias of is_int(). It also checks if a variable is an integer.
Example:
<?php
$value = 73;
if (is_integer($value)) {
echo "$value is an integer.";
} else {
echo "$value is not an integer.";
}
?>
The output of the above example is:
73 is an integer.
3. PHP is_long() Function
Another alias of is_int(). It checks if a variable is an integer.
<?php
$value = 73;
if (is_integer($value)) {
echo "$value is an integer.";
} else {
echo "$value is not an integer.";
}
?>
The output of the above example is:
-15 is an integer.
PHP Floats
A float in PHP is a type of variable that can store numbers with decimal points or numbers in exponential form. Examples of floats are 2.0, 256.4, 10.358, 7.64E+5, and 5.56E-5.
PHP Float Size and Precision
- Size: PHP's float data type can commonly store a value up to approximately 1.7976931348623E+308 (though it's platform-dependent).
- Precision: It has a maximum precision of 14 digits, meaning it can accurately represent numbers with up to 14 decimal places.
PHP Float Predefined Constants
PHP provides some predefined constants related to floats:
- PHP_FLOAT_MAX: This represents the largest possible floating-point number.
- PHP_FLOAT_MIN: This represents the smallest positive floating-point number.
- PHP_FLOAT_DIG: It indicates the number of decimal digits that can be rounded into a float and back without losing precision.
- PHP_FLOAT_EPSILON: It represents the smallest positive number 'x' such that adding 1.0 to 'x' does not result in 1.0.
PHP Functions to Check Float Type
PHP provides functions to check if a variable is of type float:
- is_float(): This function checks if a variable is of the float type.
- is_double(): It's an alias of is_float(). It also checks if a variable is of the float type.
PHP Float Examples
Let's look at some examples.
Example 1
<?php
$number1 = 2.0;
$number2 = 7.64e5;
// Performing a calculation with floats
$result = $number1 * $number2;
echo $result;
?>
The output of the above example is:
1528000
Example 2
<?php
echo "PHP_FLOAT_MAX: " . PHP_FLOAT_MAX . "<br/>";
echo "PHP_FLOAT_MIN: " . PHP_FLOAT_MIN . "<br/>";
echo "PHP_FLOAT_DIG: " . PHP_FLOAT_DIG . "<br/>";
echo "PHP_FLOAT_EPSILON: " . PHP_FLOAT_EPSILON . "<br/>";
?>
The output of the above example is:
PHP_FLOAT_MAX: 1.7976931348623E+308
PHP_FLOAT_MIN: 2.2250738585072E-308
PHP_FLOAT_DIG: 15
PHP_FLOAT_EPSILON: 2.2204460492503E-16
Example 3
<?php
$floatVar = 10.5;
// Using is_float()
if (is_float($floatVar)) {
echo "It's a float! . <br>";
} else {
echo "It's not a float!";
}
if (is_double($floatVar)) {
echo "It's a double!";
} else {
echo "It's not a double!";
}
?>
The output of the above example is:
It's a float! .
It's a double!
PHP Infinity
In PHP, when a numeric value becomes larger than the maximum representable floating-point number (PHP_FLOAT_MAX), it is considered infinite. This is a way to represent values that are so large.
Checking for PHP Finite and Infinite Values
PHP provides two functions to check whether a numeric value is finite or infinite:
- is_finite(): This function checks if a numeric value is finite, meaning it is a regular, finite number and not infinite.
- is_infinite(): This function checks if a numeric value is infinite.
PHP Infinity Examples
Let's have a look at some examples.
Example 1
<?php
$finiteValue = 123.45;
if (is_finite($finiteValue)) {
echo "The value is finite!";
} else {
echo "The value is not finite!";
}
?>
The output of the above example is:
The value is finite!
In this example, since $finiteValue is a regular finite number, the output will be "The value is finite!"
Example 2
<?php
$infiniteValue = 1.0 / 0.0;
if (is_infinite($infiniteValue)) {
echo "The value is infinite!";
} else {
echo "The value is not infinite!";
}
?>
In this example, $infiniteValue is a result of dividing 1.0 by 0.0, which results in an infinite value. The output will be "The value is infinite!"
Note: These functions are useful when dealing with calculations that might result in very large or infinite values, allowing you to check and handle such cases in your PHP code.
PHP Numerical Strings
In PHP, a numerical string is a string that represents a numeric value, such as "123" or "-45.67". It's a way of storing numbers as text. The is_numeric() function is used to determine whether a variable contains a numeric value or a numeric string.
Using is_numeric()
The is_numeric() function checks if a variable is numeric. It returns true if the variable is a number or a numeric string and false otherwise.
PHP Numerical Strings Examples
Let's have a look at the examples to understand better.
Example 1
<?php
$numericVar = 42;
if (is_numeric($numericVar)) {
echo "The variable is numeric!";
} else {
echo "The variable is not numeric!";
}
?>
The output of the above example is:
The variable is numeric!
In this example, $numericVar is assigned the numeric value 42. The is_numeric() function checks if it's numeric, and since it is, the output will be "The variable is numeric!".
Example 2: Numeric String
<?php
$numericString = "123.45";
if (is_numeric($numericString)) {
echo "The variable is numeric!";
} else {
echo "The variable is not numeric!";
}
?>
The output of the above example is:
The variable is numeric!
Here, $numericString is a string that represents the numeric value 123.45. The is_numeric() function checks if it's numeric, and as it is, the output will be "The variable is numeric!"
These examples illustrate how the is_numeric() function can be used to determine if a variable contains a numeric value or a numeric string in PHP.
PHP NaN
NaN stands for "Not a Number." In PHP, NaN is used to represent the result of impossible mathematical operations, such as dividing zero by zero. It signifies that a value is not a valid numeric result.
Using is_nan()
The is_nan() This function checks if a given value is NaN. It returns true if the value is NaN and false otherwise.
PHP NaN Examples
Let's explore examples on PHP NaN.
Example 1: NaN Value
<?php
$notANumber = acos(2);
if (is_nan($notANumber)) {
echo "The value is Not a Number (NaN)!";
} else {
echo "The value is a valid number!";
}
?>
The output of the above example is:
The value is Not a Number (NaN)!
In this example, acos(2) is an impossible mathematical operation, resulting in NaN. The is_nan() function checks if the value is NaN, and as it is, the output will be "The value is Not a Number (NaN)!"
Example 2: Valid Number
<?php
$validNumber = 123;
if (is_nan($validNumber)) {
echo "The value is Not a Number (NaN)!";
} else {
echo "The value is a valid number!";
}
?>
The output of the above example is:
The value is a valid number!
Here, $validNumber is a regular numeric value. The is_nan() function checks if it's NaN, and since it's not, the output will be "The value is a valid number!"
These examples demonstrate how the is_nan() function can be used to identify whether a given value is NaN in PHP.