### Introduction

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 developers may need in their daily routine.

### 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.

- decimal (10-based)
- hexadecimal (16-based – prefixed with 0x)
- octal (8-based – prefixed with 0)

**Note: PHP provides built-in constants for integers.**

Constant | Returns |

PHP_INT_MAX | Largest integer supported in PHP |

PHP_INT_MIN | Smallest integer supported in PHP |

PHP_INT_SIZE | Size 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.

- is_int()
- is_integer()
- is_long()

```
<?php
$a = 9012;
var_dump(is_int($a));
$b = 129.75;
var_dump(is_int($b));
?>
```

**How to convert a string in 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 depends 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.

Function | Description |

PHP_FLOAT_MAX | Largest floating-point number |

PHP_FLOAT_MIN | A smallest positive floating-point number |

PHP_FLOAT_MAX | A smallest negative floating-point number |

PHP_FLOAT_DIG | Number of decimal digits that can be rounded to float |

PHP_FLOAT_EPSILON | Smallest positive number y, so that y + 1.0 != 1.0 |

**How to check if a variable is afloat in PHP?**

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

```
<?php
$a = 12.65;
var_dump(is_float($a));
?>
```

**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 as 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 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 produce critical flaws in the output.

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

```
<?php
$a = 515;
var_dump(is_numeric($a));
$a = "185";
var_dump(is_numeric($a));
$a = "12.85" + 10;
var_dump(is_numeric($a));
$a = "Hi!";
var_dump(is_numeric($a));
?>
```

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**

```
<?php
echo(round(0.70)); // returns 1
echo(round(0.39)); // returns 0
?>
```

**Generate a random number with PHP rand() function**

```
<?php
echo(rand());
?>
```

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

```
<?php
echo(sqrt(4)); // returns 2
?>
```

**Get a maximum or minimum number in PHP**

```
<?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.