### PHP Tutorial

PHP 7 Math Functions

Updated Date:-01-02-2020

PHP provides several built-in math functions, which are used to perform mathematical tasks in the PHP program. These functions make it easier to perform mathematical operations.

There are some important math functions following

## PHP max() function

The PHP max() function is used to get the highest value from the values ​​used in an array.

Syntax:

```    max( \$array );

OR

max( value1, value2, value3 );```

### Example of PHP max() function

Let's create a program to get the highest value with the PHP max() function.

```    <!DOCTYPE html>
<html>
<body>

<?php

\$array1 = array( 5, 10, 15, 20 );

\$array2 = array( 10, 20, 30, 40 );

echo max( \$array1 ) ."<br>";  // get highest value from array

echo max( \$array2 ) ."<br>";

echo max( 20,35,40,55 ) ."<br>";  // get directly highest value from array values

?>

</body>
</html>```

## PHP min() function

The PHP Max () function is used to get the lowest/minimum value from the array. And you can also pass direct values or array in PHP min() function.

Syntax:

```    min( \$array );

OR

min( value1, value2, value3 );```

### Example of PHP min() function

Let's create a program to get the lowest value with the PHP max() function.

```    <!DOCTYPE html>
<html>
<body>

<?php

\$array1 = array( 20, 40, 60, 80 );

\$array2 = array( 7, 14, 21, 28 );

echo min( \$array1 ) ."<br>";  // get lowest value from array

echo min( \$array2 ) ."<br>";

echo min( 15,30,45,60 ) ."<br>";  // get directly lowest value from array values

?>

</body>
</html>```

## PHP abs() function

With the PHP abs () function, we can get the absolute value of a value/number. The positive form of a number is obtained by this function.

Syntax:

`    abs( \$number );`

### Example of PHP abs() function

Let's create a program to get absolute value with PHP abs() function.

```    <!DOCTYPE html>
<html>
<body>

<?php

\$number1 = 9 ;         // Positive Integer value.
\$number2 = -9 ;     // Negative Integer value.
\$number3 = 2.5 ;     // Positive Float value.
\$number4 = -3.7;     // Negative Float value.

# Print all absolute numbers
echo abs( \$number1 ) ."<br>";
echo abs( \$number2 ) ."<br>" ;
echo abs( \$number3 ) ."<br>";
echo abs( \$number4 ) ."<br>";

?>

</body>
</html>```

## PHP round() function

The PHP round() function is used to rounds a float/double value.

Syntax:

`    round( \$number );`

### Example of PHP round() function

Let's create a program to get round values with PHP round value.

```    <!DOCTYPE html>
<html>
<body>

<?php

// Use some float/double pointer numbers
\$number1 = 0.30 ;
\$number2 = 0.75 ;
\$number3 = 6.89 ;
\$number4 = 8.21 ;

// Print round values
echo round( \$number1 ) . "<br>" ;
echo round( \$number2 ) . "<br>" ;
echo round( \$number3 ) . "<br>" ;
echo round( \$number4 ) . "<br>" ;

?>

</body>
</html>```

## PHP ceil() function

The PHP floor () function is used to round values to the highest integer value.

Syntax:

`    ceil( \$number ) ;`

### Example of PHP ceil() function

Let's create a program to get round values to the highest integer value with PHP ceil() function.

```    <!DOCTYPE html>
<html>
<body>

<?php

// Use some float numbers
\$number1 = 0.21 ;
\$number2 = 9.38 ;
\$number3 = 5.45 ;

// Print round values
echo ceil( \$number1 ) . "<br>" ;
echo ceil( \$number2 ) . "<br>" ;
echo ceil( \$number3 ) . "<br>" ;

?>

</body>
</html>```

## PHP floor() function

The PHP floor () function is used to round values to the lowest integer value.

Syntax:

`    floor( \$number ) ;`

### Example of PHP floor() function

Let's create a program to get round values to the lowest integer value with the PHP floor() function.

```    <!DOCTYPE html>
<html>
<body>

<?php

// Use some float numbers
\$number1 = 3.6 ;
\$number2 = 2.47 ;
\$number3 = 6.9 ;

// Print round values
echo floor( \$number1 ) . "<br>";
echo floor( \$number2 ) . "<br>";
echo floor( \$number3 ) . "<br>";

?>

</body>
</html>```

## PHP rand() function

The PHP rand () function is used to generate a random integer number by a given range. If you do not pass a value in this function it automatically generates a random number between 8 and 10 digits.

Syntax:

```    rand( );

OR

rand ( min_value , max_value );```

### Example of PHP rand() function

Let's create a program to generate random numbers with PHP rand() function.

```    <!DOCTYPE html>
<html>
<body>

<?php

# Assigned two varriable for Minimum and Maximum values

\$min_value = 5;
\$max_value = 20;

# Print generated random integers

echo rand() ."<br>"; // Automatically generate random integer values

echo rand( \$min_value,\$max_value ) ."<br>"; // Generate integer values by a given range

?>

</body>
</html>```

## PHP mt_rand() function

The PHP mt_rand () function acts like the rand () function. But the mt_rand function used the Mersenne Twister algorithm to generate random integer values ​​/ numbers. that function is faster than rand() function.

Syntax:

```    mt_rand( );

OR

mt_rand ( min_value , max_value );```

### Example of PHP mt_rand() function

Let's create a program to generate random integers with PHP mt_rand() function.

```    <!DOCTYPE html>
<html>
<body>

<?php

# Assigned two varriable for Minimum and Maximum values

\$min_value = 5;
\$max_value = 10;

# Print generated random integers

echo mt_rand() ."<br>"; // Automatically generate random integer values

echo mt_rand( \$min_value,\$max_value ) ."<br>"; // Generate integer values by a given range

?>

</body>
</html>```

## PHP sqrt() function

The PHP sqrt() function is used to derive the square root of a number.

Syntax:

`   sqrt( \$number );`

### Example of PHP sqrt() function

Let's create a program to get the square root of a number.

```    <!DOCTYPE html>
<html>
<body>

<?php

// Use some numbers
\$number1 = 16 ;
\$number2 = 4.6 ;
\$number3 = 28 ;
\$number4 = -36 ;

// Print the square roots
echo sqrt( \$number1 ) . "<br>" ;
echo sqrt( \$number2 ) . "<br>" ;
echo sqrt( \$number3 ) . "<br>" ;
echo sqrt( \$number4 ) . "<br>" ;

?>

</body>
</html>```

## PHP is_nan() function

The PHP is_nan function is used to check a value. In which it is shown whether the given value is a number or not. If the given value is not a number, it returns true(1) otherwise it returns false/nothing.

Syntax:

`   is_nan( \$number );`

### Example of PHP is_nan() function

Let's create a program to check the given value is a number or not with PHP is_nan() function.

```    <!DOCTYPE html>
<html>
<body>

<?php

echo is_nan(358) . "<br>"; // value is a number.

echo is_nan(acos(2)) ."<br>"; // value is a function.

?>

</body>
</html>```

## PHP bindec() function

The PHP bindec () function is used to convert a binary number into a decimal number.

Syntax:

`    bindec( \$number );`

### Example of PHP bindec() function

Let's create a program to convert binary numbers to decimal numbers with PHP bindec() function.

```    <!DOCTYPE html>
<html>
<body>

<?php

// Use some binary numbers
\$number1 = 0110 ;
\$number2 = 1010 ;
\$number3 = 101011100101 ;

// Print decimal numbers
echo bindec( \$number1 ) . "<br>";
echo bindec( \$number2 ) . "<br>";
echo bindec( \$number3 ) . "<br>";

?>

</body>
</html>```

## PHP decbin() function

The PHP decbin () function is used to convert a decimal number into a binary number.

Syntax:

`   decbin( \$number );`

### Example of PHP decbin() function

Let's create a program to convert decimal number to binary number with PHP decbin() function.

```    <!DOCTYPE html>
<html>
<body>

<?php

// Use some decimal numbers
\$number1 = 10 ;
\$number2 = 25 ;
\$number3 = 47 ;

// Print binary numbers
echo decbin( \$number1 ) . "<br>";
echo decbin( \$number2 ) . "<br>";
echo decbin( \$number3 ) . "<br>";

?>

</body>
</html>```

## PHP decoct() function

The PHP decoct () function is used to convert a decimal number into an octal number.

Syntax:

`    decoct( \$number );`

### Example of PHP decoct() function

Let's create a program to convert decimal numbers to the octal number with the PHP decoct() function.

```    <!DOCTYPE html>
<html>
<body>

<?php

// Use some decimal numbers
\$number1 = 120 ;
\$number2 = 245 ;
\$number3 = 196 ;

// Print octal numbers
echo decoct( \$number1 ) . "<br>";
echo decoct( \$number2 ) . "<br>";
echo decoct( \$number3 ) . "<br>";

?>

</body>
</html>```

## PHP dechex() function

The PHP dechex () function is used to convert a decimal number into a hexadecimal number.

Syntax:

`    dechex( \$number );`

### Example of PHP dechex() function

Let's create a program to convert the decimal numbers to the hexadecimal number with the PHP dechex() function.

```    <!DOCTYPE html>
<html>
<body>

<?php

// Use some decimal numbers
\$number1 = 2035 ;
\$number2 = 19057 ;
\$number3 = 24356 ;

echo dechex( \$number1 ) . "<br>";
echo dechex( \$number2) . "<br>";
echo dechex( \$number3 ) . "<br>";

?>

</body>
</html>```

## PHP hexdec() function

The PHP hexdec() function is used to convert a hexadecimal number into a decimal number.

Syntax:

`    hexdec( '\$number' );`

### Example of PHP hexdec() function

Let's create a program to convert the hexadecimal number to the decimal number with the PHP hexdec() function.

```    <!DOCTYPE html>
<html>
<body>

<?php

\$number1 = 'FF22' ;
\$number2 = 'A34D' ;
\$number3 = 'E25D05' ;

// Print decimal numbers
echo hexdec( \$number1 ) . "<br>";
echo hexdec( \$number2) . "<br>";
echo hexdec( \$number3 ) . "<br>";

?>

</body>
</html>```

## PHP octdec() function

The PHP octdec() function is used to convert a octal number into a decimal number.

Syntax:

`    octdec( \$number );`

### Example of PHP octdec() function

Let's create a program to convert the octal number to decimal number with the PHP octdec() function.

```    <!DOCTYPE html>
<html>
<body>

<?php

// Use some octal numbers
\$number1 = 554 ;
\$number2 = 3543 ;
\$number3 = 345 ;

//Print decimal numbers
echo octdec( \$number1 ) . "<br>";
echo octdec( \$number2) . "<br>";
echo octdec( \$number3 ) . "<br>";

?>

</body>
</html>```