PHP Numbers and Math Functions

By Vijay

By Vijay

Image
I'm Vijay, and I've been working on this blog for the past 20+ years! I’ve been in the IT industry for more than 20 years now. I completed my graduation in B.E. Computer Science from a reputed Pune university and then started my career in…

Learn about our editorial policies.
Updated July 4, 2025
Edited by Kamila

Edited by Kamila

Image
Kamila is an AI-based technical expert, author, and trainer with a Master’s degree in CRM. She has over 15 years of work experience in several top-notch IT companies. She has published more than 500 articles on various Software Testing Related Topics, Programming Languages, AI Concepts,…

Learn about our editorial policies.

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

PHP Numbers & Mathematical Functions

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,

  1. Integers
  2. Floats (Floating-point numbers)
  3. Numerical Strings

In a nutshell,

TypeDescription
1IntegerA whole number, without a decimal point.
2Float (Floating-point number)A number with a decimal point.
3Numerical stringA 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:

  1. Decimal (base 10)
  2. Hexadecimal (base 16)
  3. Octal (base 8)
  4. Binary (base 2)

The following table shows the numbers 0 to 25 in decimal, hexadecimal, octal, and binary.

DecimalHexadecimalOctalBinary
0000
1111
22210
33311
444100
555101
666110
777111
88101000
99111001
10A121010
11B131011
12C141100
13D151101
14E161110
15F171111
16102010000
17112110001
18122210010
19132310011
20142410100
21152510101
22162610110
23172710111
24183011000
25193111001

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:

  1. abs() – Outputs the absolute value of a number.
  2. acos() – Outputs the arc cosine of a number in radians.
  3. acosh() – Outputs the inverse hyperbolic cosine of a number.
  4. asin() – Outputs the arc sine of a number in radians.
  5. asinh() – Outputs the inverse hyperbolic sine of a number.
  6. atan2() – Calculates the arc tangent of two variables.
  7. atan() – Outputs the arc tangent of a number in radians.
  8. atanh() – Outputs the inverse hyperbolic tangent of a number.
  9. base_convert() – Convert a number between arbitrary bases.
  10. bindec() – Converts a binary to a decimal number.
  11. ceil() – Rounding up a number to the nearest integer value.
  12. cos() – Outputs the cosine of a number.
  13. cosh() – Outputs the hyperbolic cosine of a number.
  14. decbin() – Converts a decimal to a binary number.
  15. dechex() – Converts a decimal to a hexadecimal number.
  16. decoct() – Converts a decimal to an octal number.
  17. deg2rad() – Converts a number in degrees to radians.
  18. exp() – Outputs e raised to the power of a number.
  19. expm1() – Returns exp(number) – 1.
  20. fdiv()* – Outputs the floating point value of dividing a number by another number.
  21. floor() – Rounding down a number to the nearest integer value.
  22. fmod() – Outputs the floating point remainder of dividing a number (dividend) by another number (divisor).
  23. getrandmax() – Outputs the largest possible random value.
  24. hexdec() – Converts hexadecimal to a decimal number.
  25. hypot() – Calculates the length of the hypotenuse of a right-angle triangle.
  26. intdiv() – Conducts integer division.
  27. is_finite() – Finds out whether a value is a finite number or not.
  28. is_infinite() – Finds out whether a value is an infinite number or not.
  29. is_nan() – Finds out whether a value is not a number
  30. lcg_value() – It acts as the combined linear congruential generator. Outputs a pseudo-random number in the range of 0 and 1.
  31. log10() – Outputs the base-10 logarithm of a number.
  32. log1p() – Outputs log(1 + number).
  33. log() – Outputs the natural logarithm of a number.
  34. max() – Outputs the highest value.
  35. min() – Outputs the lowest value.
  36. mt_getrandmax() – Outputs the largest possible random value.
  37. mt_rand() – Create a random integer via the Mersenne Twister Random Number Generator.
  38. mt_srand() – Seeds the Mersenne Twister Random Number Generator.
  39. octdec()- Converts an octal to a decimal number.
  40. pi() – Outputs the value of pi.
  41. pow() – It acts as an exponential expression. Outputs a number raised to the power of the exponent.
  42. rad2deg() – Converts a radian number to the equivalent number in degrees
  43. rand() – Generate a random integer
  44. round() – Rounds a floating-point number.
  45. sin() – Outputs sine of a number.
  46. sinh() – Outputs hyperbolic sine of a number.
  47. sqrt() – Outputs the square root of a number.
  48. srand() – Seeds the random number generator.
  49. tan() – Outputs the tangent of a number.
  50. 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.

PREV Tutorial | NEXT Tutorial

Was this helpful?

Thanks for your feedback!

READ MORE FROM THIS SERIES:



Leave a Comment