Math functions in PHP

Operators in PHP
Loops in PHP

You will learn PHP numbers and useful math functions available in PHP 8. We will see the Number strings, Floating Numbers, and Integers first. In the next section, we’ll explore useful math functions that a PHP developer may need in their daily routine.

This article will cover the following important questions relating to PHP numbers and Math.

  1. Show a number to two decimal places
  2. How to generate a random, unique, alphanumeric string?
  3. Numbers to Roman Numbers with PHP
  4. How can I add commas to numbers in PHP
  5. Extract php numbers from strings
  6. Convert number to month name in PHP
  7. How can I sort arrays and data in PHP?
  8. Calculate math expression from a string using eval
  9. Converting float decimal to fraction

Integers in PHP

What are integers?

Integers are numbers that can be either positive or negative without a decimal point. The decimal point in a number comes under the float category which we’ll explore later, however, 0 is also an integer.

What is the largest and smallest integer in PHP?

For the 32-bit systems, the largest integer in PHP is 2147483647 and the smallest integer is -2147483648.

For the 64-bit systems, the largest integer in PHP is 9223372036854775807 and the smallest integer is -9223372036854775808.

If you use a number out of the range described above, it will be treated as a floating number because it exceeds the limit of memory available to store an integer.

Rules for Integers in PHP

  • An integer must be either positive or negative. However, it can be 0.
  • The integer must contain at least one digit and must not contain a decimal point.

Formats of integers in PHP

There are three formats of integers in PHP.

  1. decimal (10-based)
  2. hexadecimal (16-based – prefixed with 0x)
  3. octal (8-based – prefixed with 0)

Note: PHP provides built-in constants for integers.

PHP_INT_MAXLargest integer supported in PHP
PHP_INT_MINThe smallest integer supported in PHP
PHP_INT_SIZESize of an integer in bytes

How to check if a variable type in integer in PHP

PHP built-in functions are there to get the type of integer variable.

$a = 9012;

$b = 129.75;

How to convert a string into an integer in PHP?

It is called typecasting. Look at the following code snippet, it’s pretty simple to convert a numeric string into an integer in PHP.

$num = "312";
$int = (int)$num;

// Cast a float to int

$a = 145.78;
$int_cast = (int)$a;
echo $int_cast;

Floats in PHP

A number with a decimal point is called a floating number. It can also be a number in exponential form. Like integers, Floats have also maximum and minimum limits in PHP depending on the platform (32-bit or 64-bit).

What is the largest float in PHP?

1.7976931348623E+308 is the smallest Float in PHP. You can get this float by using PHP built-in functions for floats listed below.

PHP_FLOAT_MAXLargest floating-point number
PHP_FLOAT_MINA smallest positive floating-point number
PHP_FLOAT_MAXA smallest negative floating-point number
PHP_FLOAT_DIGNumber of decimal digits that can be rounded to float
PHP_FLOAT_EPSILONSmallest positive number y, so that   y + 1.0 != 1.0

How to check if a variable is a float in PHP?

is_float() and is_double() functions returns true if the variable given to them as a parameter is of type float.

$a = 12.65;

What is PHP var_dump() function?

var_dump() is a very useful PHP function that gives information about a variable. It returns the type of variable and its value. In the case of Arrays and Objects, var_dump iterates through all the values and returns in a structural form to display information in a descriptive way.

PHP Infinity

As we mentioned above about the PHP_FLOAT_MAX function. It returns the largest supported value in PHP. However, if a number is greater than this, it is considered an Infinity in PHP. You can check if a number is finite in PHP or not using is_finite() and is_infinite() functions.

When do you get a NAN (Not a number) in PHP?

Not A Number is a constant returned as a result of an impossible mathematical calculation. For example, division by 0 is impossible and is NaN in PHP. is_nan() function checks a calculation and returns true or false accordingly.

PHP Number Strings

PHP is a loosely typed language which means variables don’t need data type at the time of declaration. They adopt the data type of data stored in them. Similarly, if your wrap a numeric value in single or double quotes, definitely it is a string now, but PHP can treat it as a numeric value in equations which sometimes produces critical flaws in the output.

Developers can always check the Number Strings if the value held by the string is a valid number or not.

$a = 515;

$a = "185";

$a = "12.85" + 10;

$a = "Hi!";

We hope that you are now super clear about the PHP numbers. Do some practice on the IDE and grab the useful math functions in PHP in this section.

Note: In PHP 8, hexadecimal numeric strings return false when fed to the is_numeric function. Hexadecimal numbers are no longer considered numeric strings in PHP 8.

PHP Round Function

echo(round(0.70));  // returns 1
echo(round(0.39));  // returns 0

Generate a random number with PHP rand() function


Take the square root of a number with sqrt() function

echo(sqrt(4));  // returns 2

Get a maximum or minimum number in PHP

echo(min(0, 50, 20, 10, -83, -100));  // returns -100
echo(max(0, 50, 30, 120, -18, -100));  // returns 120

Similarly, abs() function returns the absolute (positive) value of a number. To explore a complete list of PHP Math functions, go ahead to this link.

In the next article, we will discuss File handling in PHP.

Operators in PHP
Loops in PHP

Tutor Network

Share this

Learn PHP from A to Z