PHP Numbers
We will examine in-depth the entities, floats, and string numbers in this chapter.
One thing to keep in mind concerning PHP is that it has built-in data type conversion.
As a result, if you give a variable an integer value, the variable's type will be an integer as well. The type will change to a string if you assign a string to the same variable.
It's possible that this automated conversion will damage your code.
PHP Integers
2, 256, -256, 10358, -179567 are all integers.
A number with no decimal portion is known as an integer.
An integer data type is a non-decimal number between -2147483648 and 2147483647 in 32 bit systems, and between -9223372036854775808 and 9223372036854775807 in 64 bit systems.
A bigger (or lower) number, since it exceeds the integer's limit, should be saved as the float.
Note :- Also vital to be aware that although 4* 2.5 is 10, the result is stored as a float as one of the operands is a float (2.5).
Here are some rules for integers :-
- A single-digit is required for an integer.
- An integer cannot contain a decimal point.
- A whole might be positive or negative.
- Entities might be defined in decimal (base 10), hexadecimal (base 16), octal notation (base 8), or binary notation (base 2).
PHP has the following predefined constants for integers:
- PHP_INT_MAX - The largest integer supported
- PHP_INT_MIN - The smallest integer supported
- PHP_INT_SIZE - The size of an integer in bytes
PHP has the following functions to check if the type of a variable is integer:
- is_int()
- is_integer() - alias of is_int()
- is_long() - alias of is_int()
Example :- Check if the type of a variable is integer :
Output :-
bool(false)
PHP Floats
A float is an exponentially formed number with a decimal dot or number.
2.0, 256.4, 10.358, 7.64E+5, 5.56E-5 are all floats.
The float data type can generally hold values up to 1,7976931348623E+308 (depending on platform) with a maximum accuracy of 14 digits.
For floats, PHP has the following built-in constants (from PHP 7.2).
- PHP_FLOAT_MAX - The largest representable floating point number.
- PHP_FLOAT_MIN - The smallest representable positive floating point number.
- -PHP_FLOAT_MAX - The smallest representable negative floating point number.
- PHP_FLOAT_DIG - The number of decimal digits that can be rounded into a float and back without precision loss.
- PHP_FLOAT_EPSILON - The smallest representable positive number x, so that x + 1.0 != 1.0.
PHP has the following functions to check if the type of a variable is float:
- is_float()
- is_double() - alias of is_float()
Example :- Check if the type of a variable is float:
Output :-
bool(true)
PHP Infinity
The value is infinite and significantly greater than PHP FLOAT MAX.
PHP includes the methods below to verify whether a number is endless or infinite :
- is_finite()
- is_infinite()
The method var_dump()
returns the data type and value, other hand.
Example :- Check if a numeric value is finite or infinite :
Output :-
Related Links
PHP NaN
Not a Number is not as NaN.
NaN is used for mathematical operations which cannot be carried out.
PHP includes these features to verify whether a value is not a number :
- is_nan()
Example :- Invalid calculation will return a NaN value :
Output :-
PHP Numerical Strings
To determine whether a variable is numeric, use the PHP is_numeric()
function.
If the variable is a number or a numeric string, the function returns true; otherwise, it returns false.
Example :- Check if the variable is numeric:
Output :-
bool(true)
bool(true)
bool(false)
Related Links
PHP Casting Strings and Floats to Integers
You may need to convert a numerical number to a different data type on occasion.
Converting a value to an integer is usually done with the (int), (integer), or intval() functions.
Example :- Cast float and string to integer :
";
// Cast string to int
$x = "23465.768";
$int_cast = (int)$x;
echo $int_cast;
?>
Output :-
23465
Note:- The integer datatype does not support decimal values.