3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php final class ArrayUtils { private static $visited = []; private static $sum = 0; private static $data = []; public static function containsSum(array $data, int $sum): bool { self::$data = $data; self::$sum = $sum; self::$visited = []; return self::found(); } private static function found(): bool { if (! self::$data) { return false; } $value = array_pop(self::$data); if (isset(self::$visited[self::$sum - $value])) { return true; } self::$visited[$value] = true; return self::found(); } } $data = range(0, 49999); shuffle($data); var_dump(ArrayUtils::containsSum($data, 1220)); var_dump(ArrayUtils::containsSum($data, -7));
based on aDjvT
Output for 7.0.28 - 7.3.0rc3
bool(true) bool(false)
Output for 5.6.38
Parse error: syntax error, unexpected ':', expecting ';' or '{' in /in/VJf7L on line 10
Process exited with code 255.