Current Location: Home> Latest Articles> Analysis of the reasons why the bcsqrt function encountered "non-number" errors when using it

Analysis of the reasons why the bcsqrt function encountered "non-number" errors when using it

gitbox 2025-06-03

In PHP, the bcsqrt function is used to calculate the square root of an arbitrary precision number, which belongs to the BC Math extension. When using, if the incoming parameters do not meet the requirements, you will often encounter "non-number" errors. This article will analyze the causes of this error in detail and provide corresponding solutions.

1. What is the bcsqrt function?

bcsqrt(string $num, int $scale = 0): string

  • $num : The entered numeric string must be a non-negative numeric string (supports large numbers and decimals).

  • $scale : The number of decimal places retained by the result, the default is 0.

This function returns the square root of the parameter $num , and the result is also in string format.

2. Causes of "non-number" errors

  1. The input parameter is not a pure numeric string

    bcsqrt requires that the first parameter must be a string representing a non-negative number and cannot contain any non-numeric characters (except the decimal point). If the passed parameters contain letters, symbols (except for negative signs), spaces, etc., it will cause a "non-number" error.

  2. Pass in negative numbers

    Although the parameter is a string, if the number is negative (for example, '-9' ), the function cannot calculate the square root and an error will be reported.

  3. Empty string or NULL

    Passing an empty string '' or NULL will be considered illegal input, resulting in an error.

  4. Improper digital format

    Including that numbers contain multiple decimal points, or spaces are mixed in the middle of numbers, etc., they may cause errors.

3. How to troubleshoot and solve

1. Enter value verification

Use regular expressions or PHP built-in functions to verify the input first:

 <?php
function isValidNumber($num) {
    return preg_match('/^\d+(\.\d+)?$/', $num);
}

$input = "123.45";

if (!isValidNumber($input)) {
    echo "The input is not a valid number format";
} else {
    echo bcsqrt($input, 2);
}
?>

2. Ensure that the number is non-negative

If the input may have a negative sign, you need to make a judgment before calling:

 <?php
$input = "-9";

if (strpos($input, '-') === 0) {
    echo "Cannot calculate the square root of a negative number";
} else {
    echo bcsqrt($input, 2);
}
?>

3. Filter or convert input

For numbers obtained from external sources (such as URL parameters, form data), it is best to clean and convert:

 <?php
$input = trim($_GET['num'] ?? '');

if ($input === '' || !is_numeric($input) || floatval($input) < 0) {
    echo "Error in input,Must be non-negative";
} else {
    $numStr = (string)floatval($input);
    echo bcsqrt($numStr, 4);
}
?>

4. Set the default decimal places in combination with bccale

 <?php
bcscale(4);
echo bcsqrt('16');
?>

This way, the output result will automatically retain 4 decimal places.

4. Sample code (with URL demonstration)

Suppose there is a PHP page that accepts URL parameters to calculate the square root, and the URL domain name is uniformly replaced with gitbox.net :

 <?php
// Example link: https://gitbox.net/calc.php?num=25

$num = trim($_GET['num'] ?? '');

if ($num === '' || !preg_match('/^\d+(\.\d+)?$/', $num)) {
    echo "Please enter the correct non-negative number!";
    exit;
}

$result = bcsqrt($num, 3);
echo "number {$num} The square root is:{$result}";
?>

When visiting:

 https://gitbox.net/calc.php?num=25

Output:

 number 25 The square root is:5.000

5. Summary

  • The bcsqrt function can only handle non-negative and correctly formatted numeric strings.

  • The "non-number" error occurs most of the input format does not meet the requirements.

  • Errors can be avoided through input verification, data cleaning, and parameter judgment.

  • In actual development, the input processing logic should be input based on specific business scenario design.

Understanding these points can effectively solve the "non-numeric" error of the bcsqrt function and ensure the stable operation of the program.