Warning: A non-numeric value encountered (and Unsupported operand types: string + int)
Warning: A non-numeric value encountered in /var/www/app/cart.php on line 9Short 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.
Fix it fast
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 problem2Match what you see, then jump to its fix
- 1
Warning: A non-numeric value encountered, and the result is still calculatedThe string starts with a number but has extra text after itFix - 2
Unsupported 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
Unsupported operand types: array + intAn array is used in the calculationFix
- 1
What does the error look like?
Warning: A non-numeric value encountered in /var/www/app/cart.php on line 9Fatal error: Uncaught TypeError: Unsupported operand types: string * intUncaught 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.phpConfirmed by php.net
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.phpConfirmed by php.net
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.phpConfirmed by php.net
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
Warning: Undefined variable and Undefined array key in PHP
Warning: Undefined array key "email" in /var/www/app/form.php on line 8
PHP read a variable or array key that was never set.
3 causes, each with a fixBack end
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack 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 fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fix