Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Warning: A non-numeric value encountered (and Unsupported operand types: string + int)

ErrorWarning: A non-numeric value encountered in /var/www/app/cart.php on line 9

Short answer

You used a string in arithmetic that is not a clean number. In PHP 8, a string that starts with a number, like '5 apples', gives this warning and uses the leading number. A string with no number, like '' or 'abc', throws a TypeError. Validate or cast the value before the calculation.

  • Applies to PHP 8.3, PHP 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Dump the exact value that reaches the calculation. Quotes in the output show hidden spaces, units or an empty string.

    <?php
    var_dump($price);   // string(5) "12.50" fine; string(0) "" or "12px" is the problem
  2. 2Match what you see, then jump to its fix

    1. 1Warning: A non-numeric value encountered, and the result is still calculatedThe string starts with a number but has extra text after itFix
    2. 2Unsupported operand types: string * int (or +, -, /), usually from a blank form field or a missing database valueThe string is empty or has no number, so PHP 8 throws a TypeErrorFix
    3. 3Unsupported operand types: array + intAn array is used in the calculationFix

What does the error look like?

  • Warning: A non-numeric value encountered in /var/www/app/cart.php on line 9
  • Fatal error: Uncaught TypeError: Unsupported operand types: string * int
  • Uncaught TypeError: Unsupported operand types: string + int

Cause 1 of 3

The string starts with a number but has extra text after it

1Check

Values like '12px', '5 apples' or '1,500' are leading-numeric: PHP 8 uses the number at the start and raises a warning. (PHP 7 only raised a notice for these.) Prices with thousands separators fall in here.

<?php
var_dump(is_numeric($value));

2Fix

Clean the string before the calculation, instead of letting PHP guess.

<?php
$clean = (float) str_replace(',', '', $value);
$total = $clean * $qty;

3Verify

The line runs without a warning.

php -d error_reporting=E_ALL -d display_errors=1 cart.php

Confirmed by php.net

Did this fix it?

Cause 2 of 3

The string is empty or has no number, so PHP 8 throws a TypeError

1Check

An empty string and strings like 'abc' are non-numeric. The manual says arithmetic on a non-numeric string throws a TypeError as of PHP 8.0. Before PHP 8 this only produced the warning.

<?php
var_dump($_POST['qty'] ?? null);

2Fix

Check with is_numeric() and give a default, or cast the value on purpose. A cast such as (int) keeps its old behaviour and returns 0 without error.

<?php
$qty = is_numeric($_POST['qty'] ?? '') ? $_POST['qty'] + 0 : 0;
$line = (int) $qty * $unit;

3Verify

Submit the form with the field empty and it runs without a fatal error.

php -l cart.php

Confirmed by php.net

Did this fix it?

Cause 3 of 3

An array is used in the calculation

1Check

Arithmetic on an array throws a TypeError. A query that returns rows, or a form field named qty[], gives you an array instead of a number.

<?php
var_dump(gettype($qty));

2Fix

Take the single value you need from the array first, or validate the input type.

<?php
$qty = is_array($qty) ? (int) reset($qty) : (int) $qty;

3Verify

The line runs with a number.

php -l cart.php

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Validate and convert request input at the edge: filter_var or an explicit (int) / (float) after is_numeric().
  • Keep money in integers (cents) or a decimal library, not in formatted strings.
  • Add declare(strict_types=1) and type hints so a wrong type fails near its source.
  • Run tests with error_reporting = E_ALL on PHP 8.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

Apache returns 403 when a URL contains %3F

a request like /page%3Fname=test is refused while /page?name=test works

Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.