Current Location: Home> Latest Articles> PHP sqrt() Function Tutorial: How to Calculate Square Roots with Examples

PHP sqrt() Function Tutorial: How to Calculate Square Roots with Examples

gitbox 2025-06-15

1. Introduction to PHP sqrt() Function

The PHP sqrt() function is used to calculate the square root of a given number. The basic syntax of the function is as follows:

    sqrt(float $num): float

Here, $num is the number whose square root you want to calculate, and the function returns the square root of $num.

2. Examples of Using PHP sqrt() Function

(1) Calculating the Square Root of a Positive Number

You can easily calculate the square root of a positive number using the PHP sqrt() function. For example:

    $num = 16;
    $result = sqrt($num);
    echo "The square root of {$num} is {$result}";  // Output: The square root of 16 is 4

In this example, the variable $num is set to 16, and the sqrt() function is called to calculate its square root. The result is 4.

(2) Calculating the Square Root of a Negative Number

When a negative number is passed to the PHP sqrt() function, it will return NAN (Not a Number). For example:

    $num = -16;
    $result = sqrt($num);
    echo "The square root of {$num} is {$result}";  // Output: The square root of -16 is NAN

In this case, the value of $num is -16, and the result is NAN because the square root of a negative number is not a real number.

(3) Using Variables as Arguments

The PHP sqrt() function can also accept variables as arguments. Here's an example:

    $num = 25;
    $result = sqrt($num);
    echo "The square root of {$num} is {$result}";  // Output: The square root of 25 is 5

In this example, the variable $num is assigned the value 25, and the sqrt() function is used to calculate its square root. The result is 5.

3. Important Considerations

(1) Parameter Types

The PHP sqrt() function only accepts float type parameters, and they must be numeric values. Otherwise, it will return NAN. For example:

    $num1 = '16';
    $num2 = 'abc';
    $result1 = sqrt($num1);
    $result2 = sqrt($num2);
    echo "The square root of {$num1} is {$result1}\n";  // Output: The square root of 16 is 4
    echo "The square root of {$num2} is {$result2}\n";  // Output: The square root of abc is NAN

In this example, $num1 is a string '16', and $num2 is 'abc'. Therefore, the second result returns NAN.

(2) Return Value Type

The return value type of the PHP sqrt() function is float, meaning the result is a floating-point number. For example:

    $num = 15;
    $result = sqrt($num);
    var_dump($result);  // Output: float(3.8729833462074)

In this example, $num is set to 15, and the sqrt() function returns the result 3.8729833462074, which is a float value.

(3) Precision Issues

Due to floating-point precision issues in computers, you should be cautious when calculating square roots for large numbers. For example:

    $num1 = 1000000000000000;
    $num2 = 1000000000000001;
    $result1 = sqrt($num1);
    $result2 = sqrt($num2);
    echo "The square root of {$num1} is {$result1}\n";  // Output: The square root of 1000000000000000 is 1000000
    echo "The square root of {$num2} is {$result2}\n";  // Output: The square root of 1000000000000001 is 1000000.0000001

In this example, $num1 and $num2 are very close in value, but due to precision issues, the square root of $num2 shows a slight difference in the result.

4. Conclusion

The PHP sqrt() function is a very useful mathematical tool for calculating square roots. When using it, ensure the parameter is of the correct type, and be mindful of the precision issues that can occur with large numbers. This article demonstrated how to use the function and handle common errors and edge cases.