array_product(): Multiplication is not supported on type object

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.

Objects are not turned to string before being cast to numeric.

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

Example

<?php

var_dump(array_product([new stdClass()] )); // returns 1

var_dump(array_product([new stdClass(), 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 objects inside the first argument.