3v4l.org

run code in 300+ PHP versions simultaneously
<?php function mkProductList($array) { // $tmpVal get product of all elements $tmpVal = 1; // Count zeros in array $cnt = count(array_keys($array, 0)); // If there are two or more zeros, then product of all elements will be zero // No make sense to do // Fill array with zeros and return it if ($cnt > 1) { $array = array_fill_keys(array_keys($array), 0); return $array; } // If there are one or no zeros, then get product of all elements, exclude zero, // cause multiply by zero equal zero foreach($array as $value) { if ($value != 0) { $tmpVal *= $value; } } // If there are one zero, then product of all elements will be zero // exclude zero position in input array, it will be product of all elements if ($cnt == 1) { $array = array_fill_keys(array_keys($array), 0); $array[array_search(0, $array)] = $tmpVal; return $array; } // If there are no zeros, then we replace velues in input array // Product of all elements we devide by current element value // Result is product of all elements, exclude current element value foreach($array as $key => $value) { $array[$key] = $tmpVal / $value; } return $array; } // tests var_dump(producePatStr(array(1,2,3,4))); var_dump(producePatStr(array())); var_dump(producePatStr(array(-0.5, 100, 20))); ?>
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.25, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
Fatal error: Uncaught Error: Call to undefined function producePatStr() in /in/QKK25:46 Stack trace: #0 {main} thrown in /in/QKK25 on line 46
Process exited with code 255.
Output for 5.5.24 - 5.5.35, 5.6.8 - 5.6.28
Fatal error: Call to undefined function producePatStr() in /in/QKK25 on line 46
Process exited with code 255.

preferences:
175.93 ms | 402 KiB | 198 Q