A non-numeric Value Encountered

This warning was introduced in PHP 7.1 as part of stricter type handling. It occurs when you attempt to perform mathematical operations (addition, subtraction, multiplication, division) on variables that contain non-numeric values (strings, arrays, objects, etc.).

Example that triggers the warning:

Copy to clipboard
$a = "hello"; 
$b = 5;
$result = $a + $b; // Warning: A non-numeric value encountered

PHP 7.1 introduced stricter notices and warnings for type conversions. An E_WARNING is emitted when the string does not contain a numeric value. In PHP 7.0 and earlier, non-numeric strings were silently converted to 0 during mathematical operations without any warnings.

Common Scenarios Include:

1. Form data operations:

Copy to clipboard
$total = $_POST['quantity'] + $_POST['price']; // If inputs are empty or non-numeric

2. Database field calculations:

$subtotal = $row[‘quantity’] * $row[‘price’]; // If DB fields contain NULL or text

3. Array operations: Form data operations:

$sum += $item[‘value’]; // If array value is string or empty

4. Configuration values:

$result = $config[‘timeout’] * 1000; // If config value is “auto” or similar

Solution 1: Type Casting (Quick Fix)

Cast the variables to integers or floats, which prevents the warning and is equivalent to the behavior in PHP 7.0 and below:

$subtotal = (int)$item[‘quantity’] * (float)$product[‘price’];

Solution 2: Using is_numeric() Check

Use the is_numeric() function to check if a variable is a number or a string representation of a number:

Copy to clipboard
if (is_numeric($value1) && is_numeric($value2)) {
    $result = $value1 + $value2;
} else {// Handle non-numeric values appropriately
    $result = 0; // or throw an error
}

Solution 3: Using intval() or floatval()

$result = intval($value1) + floatval($value2); //convert a string to an integer or float

Solution 4: Null Coalescing with Default Values

$result = ($value1 ?? 0) + ($value2 ?? 0);

Solution 5: Comprehensive Validation Function

Copy to clipboard
function safeAdd($a, $b) {
    $a = is_numeric($a) ? (float)$a : 0;
    $b = is_numeric($b) ? (float)$b : 0;
    return $a + $b; 
}

Best Practices to Prevent this Warning

1. Always validate input data:

$input = filter_input(INPUT_POST, ‘amount’, FILTER_VALIDATE_FLOAT);

2. Use strict type checking:

if (!is_numeric($value)) {
throw new InvalidArgumentException(‘Expected numeric value’);
}

3. Implement proper error handling:

Copy to clipboard
function calculateTotal($items) { 
      $total = 0;
      foreach ($items as $item) { 
             if (!isset($item['price']) || !is_numeric($item['price'])) { 
                 throw new InvalidArgumentException('Invalid price value');
              } 
       $total += (float)$item['price']; } return $total; 
}

4. Use type declarations (PHP 7+):

function multiply(float $a, float $b): float {
return $a * $b;
}

5. Sanitize database results:

$price = is_numeric($row[‘price’]) ? (float)$row[‘price’] : 0.0;

Version-specific behavior:

  • PHP < 7.1: Silent conversion, no warnings
  • PHP 7.1+: E_WARNING for non-numeric strings
  • PHP 8.0+: Even stricter type handling with more notices

For maximum compatibility, always validate numeric operations regardless of PHP version.

Summary

The “Warning: A non-numeric value encountered” is PHP 7.1+’s way of helping developers write more robust code. While it can be annoying when upgrading legacy applications, addressing it properly leads to more reliable and predictable code behavior. Always prefer proper validation and type checking over warning suppression.

Need Help With Laravel Development?

Work with our skilled Laravel developers to accelerate your project and boost its performance.

Support On Demand!