array_product(): Multiplication is not supported on type string

Description

array_product() multiplies each element in the first argument, with each other. This is a valid operation for integer, floats, numeric strings, booleans and null, as they can be cast to numbers.

This is not the case for arrays, objects, non-numeric strings or resources, which yield this warning, and are omitted in the operation.

Until PHP 8.3, the omission of invalid argument was a silent behavior.

Example

<?php

var_dump(array_product(['abc'] )); // returns 1

var_dump(array_product(['33', 3] )); // returns 99

?>

Solutions

  • Filter out all values in the first argument not actually numeric: aka, they can’t be cast to integer or float without error.

  • Filter out all strings inside the first argument, which fails the is_numeric() call.