# Miscellaneous Maths Functions in PHP

Author — Nitish Kumar

## PHP `deg2rad()` Function

### PHP `deg2rad()` Usage

The PHP `deg2rad()` function will converts the number in degrees to the radian equivalent .

### PHP `deg2rad()` Syntax

`` deg2rad ( float \$number ) : float``

### PHP `deg2rad()` Parameters

1. `number` — Angular value in degrees

### PHP `deg2rad()` Return Value

The PHP `deg2rad()` function returns the radian equivalent of `number`

### PHP `deg2rad()` Working Examples

``````<?php
?>``````

## PHP `hypot()` Function

### PHP `hypot()` Usage

The PHP `hypot()` function will calculate the length of the hypotenuse of a right-angle triangle .

### PHP `hypot()` Syntax

`` hypot ( float \$x , float \$y ) : float``

### PHP `hypot()` Parameters

1. `x` — Length of first side

2. `y` — Length of second side

### PHP `hypot()` Return Value

The PHP `hypot()` function returns calculated length of the hypotenuse

## PHP `intdiv()` Function

### PHP `intdiv()` Usage

The PHP `intdiv()` function will give you the integer quotient of the division of `dividend` by `divisor`.

### PHP `intdiv()` Syntax

`` intdiv ( int \$dividend , int \$divisor ) : int``

### PHP `intdiv()` Parameters

1. `dividend` — Number to be divided.

2. `divisor` — Number which divides the dividend.

### PHP `intdiv()` Return Value

The PHP `intdiv()` function returns the integer quotient of the division of `dividend` by `divisor`.

### PHP `intdiv()` Working Examples

#### 1. intdiv() example

``````<?php
var_dump(intdiv(3, 2));
var_dump(intdiv(-3, 2));
var_dump(intdiv(3, -2));
var_dump(intdiv(-3, -2));
var_dump(intdiv(PHP_INT_MAX, PHP_INT_MAX));
var_dump(intdiv(PHP_INT_MIN, PHP_INT_MIN));
var_dump(intdiv(PHP_INT_MIN, -1));
var_dump(intdiv(1, 0));
?>``````

Output of the above code:

``````int(1)
int(-1)
int(-1)
int(1)
int(1)
int(1)
Fatal error: Uncaught ArithmeticError: Division of PHP_INT_MIN by -1 is not an integer in %s on line 8
Fatal error: Uncaught DivisionByZeroError: Division by zero in %s on line 9``````

### Additional Tips from Fellow Developers

Contributed By: AmeenRoss

``````This does indeed seem to be equal to intdiv:
<?php
function intdiv_1(\$a, \$b){
return (\$a - \$a % \$b) / \$b;
}
?>
However, this isn't:
<?php
function intdiv_2(\$a, \$b){
return floor(\$a / \$b);
}
?>
Consider an example where either of the parameters is negative:
<?php
\$param1 = -10;
\$param2 = 3;
print_r([
'modulus' => intdiv_1(\$param1, \$param2),
'floor' => intdiv_2(\$param1, \$param2),
]);
/**
* Array
* (
*     [modulus] => -3
*     [floor] => -4
* )
*/
?>``````

## PHP `lcg_value()` Function

### PHP `lcg_value()` Usage

The PHP `lcg_value()` function will give you a pseudo random number in the range of (0, 1). The function combines two CGs with periods of 2^31 - 85 and 2^31 - 249. The period of this function is equal to the product of both primes.

### PHP `lcg_value()` Syntax

`` lcg_value ( void ) : float``

### PHP `lcg_value()` Parameters

This function does not accept any parameters.

### PHP `lcg_value()` Return Value

The PHP `lcg_value()` function returns a pseudo random float value between 0.0 and 1.0, inclusive.

### Important Points about PHP `lcg_value()` Function

1. This function does not generate cryptographically secure values, and should not be used for cryptographic purposes. If you need a cryptographically secure value, consider using `random_int()`, `random_bytes()`, or `openssl_random_pseudo_bytes()` instead.

### Additional Tips from Fellow Developers

Contributed By: rok kralj gmail com

``````An elegant way to return random float between two numbers:
<?php
function random_float (\$min,\$max) {
return (\$min+lcg_value()*(abs(\$max-\$min)));
}
?>``````

Contributed By: daniel dot baulig

``````Choose your weapon:
<?php
function mt_randf(\$min, \$max)
{
return \$min + abs(\$max - \$min) * mt_rand(0, mt_getrandmax())/mt_getrandmax();
}
function lcg_randf(\$min, \$max)
{
return \$min + lcg_value() * abs(\$max - \$min);
}
function randf(\$min, \$max)
{
return \$min + rand(0,getrandmax()) / getrandmax() * abs(\$max - \$min);
}?>``````

## PHP `pi()` Function

### PHP `pi()` Usage

The PHP `pi()` function will give you an approximation of pi. Also, you can use the `M_PI` constant which yields identical results to `pi()`.

### PHP `pi()` Syntax

`` pi ( void ) : float``

### PHP `pi()` Parameters

This function does not accept any parameters.

### PHP `pi()` Return Value

The PHP `pi()` function returns the value of pi as float.

### PHP `pi()` Working Examples

#### 1. pi() example

``````<?php
echo pi(); // 3.1415926535898
echo M_PI; // 3.1415926535898
?>``````

## PHP `rad2deg()` Function

### PHP `rad2deg()` Usage

The PHP `rad2deg()` function will converts the radian number to the equivalent number in degrees .

### PHP `rad2deg()` Syntax

`` rad2deg ( float \$number ) : float``

### PHP `rad2deg()` Parameters

1. `number` — A radian value

### PHP `rad2deg()` Return Value

The PHP `rad2deg()` function returns the equivalent of `number` in degrees

### PHP `rad2deg()` Working Examples

``````<?php
?>``````

Rate this post —

(No Ratings Yet)