Advertisement

Previous Next

**PHP - Math Functions**

Previous Next

Previous Next

> A function in programing is method in a program specifically designed to perform a specific operation or task.

> Basically a function has got 3 parts that is, **arguments**, **what to do** and **what to return**.

> However, it is not necessary to supply arguments if not required. To effect the performance of a function, it must be called by just its name followed by brackets and in then supplied with arguments if required.

> Quite often PHP has got in-build math functions which can help you to do tasks quickly without writing your own function.

> Let us an example where we write our own function which calculates the square-root of a number between 0 and 100 and then see how efficient it could have been if we used the PHP sqrt() method.

> **Our own function example**

```
```
**
<?php**

$answer = 0;

function my_sqrt($number)

{

// declaring that the variable $answer is global ,i.e. can be used by this function

global $answer;

for($m = 0 ; $m <= 100 ; $m++)

{

if($m * $m == $number)

{

// if a number is multiplied by itself and gives the result as the number we are to find its square root

$answer = $m;

}

}

echo $answer;

}

my_sqrt(64); // this is how we call a function

?>

**You will get Output on browser as shown below;**

```
```
**
8
**

> However, if we could have used the in-build PHP **sqrt()** function it could have reduced out work. i.e.consider the same example but without using our own example,here we are using PHP **sqrt()** function.

> **PHP sqrt() function example**.

```
```
**
<?php**

echo sqrt(64); // this is how we call a function

?>

**You will get same Output on browser as shown below;**

```
```
**
8
**

> Some of the in-build PHP math functions are explained below;

** abs() Function**

> This function is used to get the absolute value of the number.

> **abs() syntax**.

```
```
**
<?php**

echo abs(argument) ;

?>

**> Let's see an example of abs() function**

```
```
**
<?php**

echo abs(-3.2); // 3.2 (double/float)

echo " ";

echo abs(6); // 6 (integer)

echo " ";

echo abs(-6); // 6 (integer)

?>

**You will get Output on browser as shown below;**

```
```
**
3.2 6 6
**

** acos() Function**

> This function returns the arc cosine of arguments in radians.

> **acos() syntax**.

```
```
**
<?php**

echo (acos(argument)) ;

?>

**> Let's see an example of acos() function **

```
```
**
<?php**

echo (acos(0.55));

?>

**You will get Output on browser as shown below;**

```
```
**
0.98843208892615
**

** acosh() Function**

> This function returns the inverse hyperbolic cosine of a number in radians.

> **acosh() syntax**.

```
```
**
<?php**

echo acosh(argument) ;

?>

**> Let's see an example of acosh() function**

```
```
**
<?php**

echo acosh(60) ;

?>

**You will get Output on browser as shown below;**

```
```
**
4.7874222911027
**

Previous Next

JQuery - Coming soon