3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php final class ArrayUtils { private static $visited = []; private static $sum = 0; public static function containsSum(array $data, int $sum): bool { self::$visited = []; self::$sum = $sum; return array_reduce($data, 'ArrayUtils::reduce', false); } private static function reduce(bool $carry, int $item): bool { $carry |= isset(self::$visited[self::$sum - $item]); self::$visited[$item] = true; return (bool) $carry; } } $data = range(0, 4999); shuffle($data); var_dump(ArrayUtils::containsSum($data, 1220)); var_dump(ArrayUtils::containsSum($data, -7));
based on aDjvT
Output for 7.0.28 - 7.2.11
bool(true) bool(false)
Output for 5.6.38
Parse error: syntax error, unexpected ':', expecting ';' or '{' in /in/OQOip on line 9
Process exited with code 255.