Ffile2fix
Sign in Get started

How to fix "Unsupported operand types" in PHP 8

Fatal error: Uncaught TypeError: Unsupported operand types: string + int in /home/user/public_html/cart.php:38

An arithmetic operator (+, -, *, /, %) received a value PHP cannot treat as a number, such as an array or a non-numeric string. PHP 7 often just warned and guessed; PHP 8 throws a TypeError and stops the script.

Also appears as: Uncaught TypeError: Unsupported operand types: array + int · Unsupported operand types: string * float · Uncaught TypeError: Unsupported operand types: null - array · Warning: A non-numeric value encountered (PHP 7.x)

Common causes

  • An empty string or text value ("", "N/A", "12,50") used in a calculation, common with form input and old plugin settings
  • An array used in arithmetic, e.g. get_option() or get_post_meta() returning an array
  • Using + to merge an array with a non-array value
  • A variable initialized as '' instead of 0 and then incremented
  • Plugins and themes written for PHP 7 running on PHP 8

How to fix it

  1. Read the operand types. The message tells you the left and right types (string + int). Find the variable on that line that holds the unexpected type.
  2. Initialize numbers as numbers. Use $total = 0; not $total = ''; and set numeric defaults when reading options or meta.
  3. Validate and cast input. Convert form and database values with (float), (int) or filter_var($v, FILTER_VALIDATE_FLOAT) after checking is_numeric(). Normalize decimal commas first.
  4. Fix arrays in arithmetic. If an operand is an array, use the specific element, array_sum() for totals, or pass $single = true to get_post_meta().
  5. Update the plugin or theme. If the file is in wp-content, update that component; PHP 8 compatibility fixes for this error are common in changelogs.
  6. Test on PHP 8 before switching. Run a compatibility scan or a staging copy on PHP 8.x before changing the live PHP version.

PHP 8

$qty   = $_POST['qty'] ?? '';
$price = (string) get_option('acme_price', '0');
$price = str_replace(',', '.', $price);

if (!is_numeric($qty) || !is_numeric($price)) {
    throw new InvalidArgumentException('Invalid number');
}
$total = (int) $qty * (float) $price;

How to stop it happening again

  • Store numeric settings as numbers, not formatted strings
  • Validate numeric input at the boundary
  • Run static analysis or a PHP 8 compatibility check before upgrades

Frequently asked questions

Why did "" + 1 work in PHP 7?

PHP 7 treated non-numeric strings as 0 with a warning. PHP 8's saner numeric strings change made them throw a TypeError instead.

Does "5" + 1 still work in PHP 8?

Yes. Numeric strings are still converted. Leading-numeric strings such as "5 apples" work with a warning, but fully non-numeric strings throw.

Can I merge arrays with +?

Yes, array + array is valid (it keeps keys from the left). The error occurs when one side is an array and the other is not.