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
- 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.
- Initialize numbers as numbers. Use $total = 0; not $total = ''; and set numeric defaults when reading options or meta.
- 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.
- 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().
- 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.
- 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