An in-depth study of PHP Numbers that includes Integer, Float & Numerical Strings. Explore the various PHP Math functions with examples to perform complex calculations:
We have already discussed some basics of PHP numbers in one of our earlier tutorials (PHP Data Types and Keywords).
In this tutorial, we are going to discuss PHP numbers (floats, integers, and numerical strings), infinity, NaN, converting numeric values to integers, PHP mathematical functions, and frequently asked questions (FAQs).
=> Read Through the Series of PHP Tutorials
Table of Contents:
PHP Numbers & Mathematical Functions

Please note that we have used PHP version 7 in all examples.
Let’s begin!
Different Types of Numbers
There are three main types of numbers in PHP. They are,
- Integers
- Floats (Floating-point numbers)
- Numerical Strings
In a nutshell,
| Type | Description | |
|---|---|---|
| 1 | Integer | A whole number, without a decimal point. |
| 2 | Float (Floating-point number) | A number with a decimal point. |
| 3 | Numerical string | A string is a series of characters that stays inside a pair of single or double-quotes. A numerical string can interpret as an integer or a float. (PHP 8.0.0) A string is considered as a numerical string on the condition that it had leading whitespaces. (before PHP 8.0.0) |
PHP Integers
An integer is a non-fractional numerical value and can be either positive or negative. It cannot contain a decimal point.
Examples:
1, 24, -7, -999, 35602
PHP integers can be specified using the following notations:
- Decimal (base 10)
- Hexadecimal (base 16)
- Octal (base 8)
- Binary (base 2)
The following table shows the numbers 0 to 25 in decimal, hexadecimal, octal, and binary.
| Decimal | Hexadecimal | Octal | Binary |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 1 | 1 | 1 | 1 |
| 2 | 2 | 2 | 10 |
| 3 | 3 | 3 | 11 |
| 4 | 4 | 4 | 100 |
| 5 | 5 | 5 | 101 |
| 6 | 6 | 6 | 110 |
| 7 | 7 | 7 | 111 |
| 8 | 8 | 10 | 1000 |
| 9 | 9 | 11 | 1001 |
| 10 | A | 12 | 1010 |
| 11 | B | 13 | 1011 |
| 12 | C | 14 | 1100 |
| 13 | D | 15 | 1101 |
| 14 | E | 16 | 1110 |
| 15 | F | 17 | 1111 |
| 16 | 10 | 20 | 10000 |
| 17 | 11 | 21 | 10001 |
| 18 | 12 | 22 | 10010 |
| 19 | 13 | 23 | 10011 |
| 20 | 14 | 24 | 10100 |
| 21 | 15 | 25 | 10101 |
| 22 | 16 | 26 | 10110 |
| 23 | 17 | 27 | 10111 |
| 24 | 18 | 30 | 11000 |
| 25 | 19 | 31 | 11001 |
From PHP 7.4.0 onwards, integer literals might have an underscore(s) (_) between digits for better readability of literals.
Example:
1_234_567
Further, the real size of the integer depends on the platform (platform-dependent).
Furthermore, the is_int() function is used to find out whether a value is an integer or not. It returns true if the value is an integer, otherwise false.
Example:
You can practice this example by running the following programming code:
<?php
$num1 = 105;
var_dump(is_int($num1));
echo "<br>";
$num2 = 105.0;
var_dump(is_int($num2));
?>
Output:
bool(true) bool(false)
Note: Alternatively, you can use the is_integer or is_long functions. They are aliases of the is_int() function.
PHP Floats
Floats are also called floating-point numbers, doubles, or real numbers. It is a number with a decimal point and can be either positive or negative. A floating-point number can be in the exponential form as well.
Examples:
1.0, 24.11, 0.75, -200.4, 1.6e3, 2.79E+5, 6.14E-23
The real size of the floating-point number depends on the platform (platform-dependent).
The is_float() function is used to find out whether a value is a float or not. It returns true if the value is afloat, otherwise false.
Example:
You can practice this example by running the following programming code:
<?php
$num1 = 5;
var_dump(is_float($num1));
echo "<br>";
$num2 = 5.23;
var_dump(is_float($num2));
?>
Output:
bool(false) bool(true)
Note: Alternatively, you can use the is_double function. It is an alias of the is_float() function.
From PHP 7.4.0 onwards, floats might have an underscore(s) (_) between digits for better readability of literals.
Example:
1_234.567
PHP Numerical Strings
Numerical strings are also known as numeric strings or number strings.
In PHP 8.0.0, a numerical string is a string that can interpret as an integer or a float.
Examples:
“3”, “1234”, “1995”
But, before PHP 8.0.0, a string is considered as a numerical string on the condition that it has leading whitespaces.
The is_numeric() function is used to find out whether a value is numeric (number or numeric string) or not. It returns true if the value is numeric, otherwise false.
Example:
You can practice this example by running the following programming code:
<?php
$var = 123;
var_dump(is_numeric($var));
echo "<br>";
$var = "123";
var_dump(is_numeric($var));
echo "<br>";
$var = "Welcome";
var_dump(is_numeric($var));
echo "<br>";
$var = "Year 2021";
var_dump(is_numeric($var));
echo "<br>";
$var = "1.23" + 1;
var_dump(is_numeric($var));
?>
Output:
bool(true) bool(true) bool(false) bool(false) bool(true)
PHP Infinity
When a numerical value is too big to suit a floating-point number on the current platform, it is considered infinite.
The is_finite() and is_infinite() functions are used to find out whether a value is finite or infinite.
The is_finite() function returns true if the value is a legal finite number within the accepted range for a floating-point number on the current platform, otherwise false.
The is_infinite() function returns true if the value is an infinite number or too big to suit into a floating-point number on the current platform, otherwise false.
Example:
You can practice this example by running the following programming code:
<?php
$num = 1.3e25;
var_dump(is_infinite($num));
echo "<br>";
$num = 1.3e500;
var_dump(is_infinite($num));
echo "<br>";
$num = 1.3e25;
var_dump(is_finite($num));
echo "<br>";
$num = 1.3e500;
var_dump(is_finite($num));
?>
Output:
bool(false) bool(true) bool(true) bool(false)
PHP NaN
NaN or NAN is abbreviated for Not a Number. Some numerical operations return NAN when they are undefined or unrepresentable.
Example:
You can practice this example by running the following programming code:
<?php
$n = acos(1.01);
var_dump($n);
?>
Output:
float(NAN)
The is_nan() function is used to find out whether a value is not a number. It returns true if the value is “not a number”, otherwise false.
Example:
You can practice this example by running the following programming code:
<?php
$val = acos(1.01);
var_dump($val, is_nan($val));
?>
Output:
float(NAN) bool(true)
Converting Numeric Values to Integers
In some situations, we need to convert a numeric value to another type.
PHP String to Int
The (int) function is used to convert a string to an int.
Example 1:
In the below example, we have converted a numerical string to an integer. You can practice this example by running the following programming code:
<?php
$str = "7.65";
echo (int)$str;
?>
Output:
7
Example 2:
In the example below, we have converted a non-numerical string to an integer. You can practice this example by running the following programming code:
<?php
$str = "abx";
echo (int)$str;
?>
Output:
0
Note: Alternatively, you can use the (integer) or intval() functions. They are aliases of the (int) function.
PHP Float to Int
The (int) function is also used to convert a float to an int.
Example:
In the example below, we have converted a floating-point number to an integer. You can practice this example by running the following programming code:
<?php
$num = 7.65;
echo (int)$num;
?>
Output:
7
Note: Alternatively, you can use the (integer) or intval() functions. They are aliases of the (int) function.
PHP Mathematical Functions
You can use operators to perform basic maths in PHP. However, to perform advanced operations, you may need mathematical functions.
The following list shows the list of PHP mathematical functions:
- abs() – Outputs the absolute value of a number.
- acos() – Outputs the arc cosine of a number in radians.
- acosh() – Outputs the inverse hyperbolic cosine of a number.
- asin() – Outputs the arc sine of a number in radians.
- asinh() – Outputs the inverse hyperbolic sine of a number.
- atan2() – Calculates the arc tangent of two variables.
- atan() – Outputs the arc tangent of a number in radians.
- atanh() – Outputs the inverse hyperbolic tangent of a number.
- base_convert() – Convert a number between arbitrary bases.
- bindec() – Converts a binary to a decimal number.
- ceil() – Rounding up a number to the nearest integer value.
- cos() – Outputs the cosine of a number.
- cosh() – Outputs the hyperbolic cosine of a number.
- decbin() – Converts a decimal to a binary number.
- dechex() – Converts a decimal to a hexadecimal number.
- decoct() – Converts a decimal to an octal number.
- deg2rad() – Converts a number in degrees to radians.
- exp() – Outputs e raised to the power of a number.
- expm1() – Returns exp(number) – 1.
- fdiv()* – Outputs the floating point value of dividing a number by another number.
- floor() – Rounding down a number to the nearest integer value.
- fmod() – Outputs the floating point remainder of dividing a number (dividend) by another number (divisor).
- getrandmax() – Outputs the largest possible random value.
- hexdec() – Converts hexadecimal to a decimal number.
- hypot() – Calculates the length of the hypotenuse of a right-angle triangle.
- intdiv() – Conducts integer division.
- is_finite() – Finds out whether a value is a finite number or not.
- is_infinite() – Finds out whether a value is an infinite number or not.
- is_nan() – Finds out whether a value is not a number
- lcg_value() – It acts as the combined linear congruential generator. Outputs a pseudo-random number in the range of 0 and 1.
- log10() – Outputs the base-10 logarithm of a number.
- log1p() – Outputs log(1 + number).
- log() – Outputs the natural logarithm of a number.
- max() – Outputs the highest value.
- min() – Outputs the lowest value.
- mt_getrandmax() – Outputs the largest possible random value.
- mt_rand() – Create a random integer via the Mersenne Twister Random Number Generator.
- mt_srand() – Seeds the Mersenne Twister Random Number Generator.
- octdec()- Converts an octal to a decimal number.
- pi() – Outputs the value of pi.
- pow() – It acts as an exponential expression. Outputs a number raised to the power of the exponent.
- rad2deg() – Converts a radian number to the equivalent number in degrees
- rand() – Generate a random integer
- round() – Rounds a floating-point number.
- sin() – Outputs sine of a number.
- sinh() – Outputs hyperbolic sine of a number.
- sqrt() – Outputs the square root of a number.
- srand() – Seeds the random number generator.
- tan() – Outputs the tangent of a number.
- tanh() – Outputs the hyperbolic tangent of a number.
* in PHP 8
Given below are some examples.
Example 1: PHP abs() function
You can practice this example by running the following programming code:
<?php
echo abs(7)."<br>";
echo abs(-7)."<br>";
echo abs(3.8)."<br>";
echo abs(-3.8)
?>
Output:
7 7 3.8 3.8
Example 2: PHP max() function
You can practice this example by running the following programming code:
<?php
echo max(33, -3, 5, 102.5, 0, -520);
?>
Output:
102.5
Example 3: PHP sin() function
You can practice this example by running the following programming code:
<?php
echo sin(60);
?>
Output:
-0.30481062110222
Note: You may refer to this link for more examples.
Frequently Asked Questions
1. What are integers?
Integers are whole numbers without decimal points and can be either positive or negative.
Examples: 25, -25
2. What is a float?
A float or a floating-point number is a number with a decimal point and can be either positive or negative, too.
Examples: 25.0, -25.0, 102.3, -3.01
3. Is there a negative integer?
Yes, an integer can be either positive or negative.
Examples: -3, -50, -1275
4. Can a float be negative?
Yes, a float can be negative too.
Examples: -3.0, -7.7, -40.275
5. How to check whether a variable is numeric or not?
The is_numeric() function can check whether a variable is numeric or not. It returns true if the value is numeric, otherwise false.
6. Does PHP round(), round up or down?
PHP round() function rounds up or down depending on the floating-point number.
Examples:
round(3.2) outputs 3
round(3.5) outputs 4
round(3.8) outputs 4
7. How can I get the value of pi?
You can use the pi() function to get the value of pi. It outputs 3.1415926535898.
8. What is the ceil() function?
It is a function for rounding up a number to the nearest integer value.
Examples:
ceil(3.2) outputs 4
ceil(3.5) outputs 4
ceil(3.8) outputs 4
Conclusion
PHP has three main types of numbers called integers, floats, and numerical strings. An integer is a whole number without a decimal point, while a float is a number with a decimal point. Both can be either positive or negative.
A numerical string is a string that can be interpreted as an integer or float. The var_dump() function can get the data type and value. PHP also has several other built-in functions to deal with numbers and complex maths.






