To find the square root of a number in php, the easiest way is with the php **sqrt()** function.

`echo sqrt(4); // Output: 2`

Finding the square root of a number in php is easy. We can find the square root of both integers and floats using the php **sqrt()** function.

In mathematics, the square root of a number a square root of a number x is a number y such that y^2 = x.

In php, we can calculate the square root of positive numbers including 0, but for negative numbers, the **sqrt()** function returns NAN.

```
echo sqrt(4); // Output: 2
echo sqrt(16); // Output: 4
echo sqrt(2); // Output: 1.4142135623731
echo sqrt(-2); // Output: NAN
```

## Finding the Square Root of Positive Number in php

In php, we can use the **sqrt()** function to find the square root of a positive number. The return value will also be of type float.

```
echo sqrt(9); // Output: 3
echo sqrt(25); // Output: 5
echo sqrt(7); // Output: 2.6457513110646
```

## Finding the Square Root of a Negative Number in php

In php, if you pass a negative number to the **sqrt()** function, you will get NAN.

To calculate the square root of a negative number in php, we can use the php absolute value function **abs()** and then calculate the square root of the absolute value.

```
echo sqrt(-91.1237); // Output: NAN
echo sqrt(abs(-91.1237)); // Output: 9.5458734540114 (Not exactly correct though, see note below)
echo sqrt(abs(91.1237)); // Output: 9.5458734540114
```

One thing to note here is that the square root of a negative number is not equal to the square root of a postive number – we still need to consider imaginary numbers.

Implementing imaginary numbers is beyond the scope of this article. Mark Baker has put together a wonderful package for complex numbers in php that you can use for working with imaginary numbers.

Hopefully this article has been helpful for you to understand how you can find the absolute value of a number in using php.

## Leave a Reply