3v4l.org

run code in 300+ PHP versions simultaneously
<?php function rotate(iterable $iterable, callable $predicate) { $tail = []; $match = false; foreach ($iterable as $key => $value) { if ($match || $predicate($value)) { yield $key => $value; $match = true; } else { $tail[] = [$key, $value]; } } foreach ($tail as [$key, $value]) { yield $key => $value; } } function gen() { yield 1; yield 2; yield 3; yield 4; yield 5; } $res = rotate(gen(), function($el) { return $el === 3; }); var_dump(iterator_to_array($res)); // [3, 4, 5, 1, 2]
Output for 7.1.0 - 7.1.33, 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.34, 8.2.0 - 8.2.30, 8.3.0 - 8.3.30, 8.4.1 - 8.4.18, 8.5.0 - 8.5.3
array(5) { [2]=> int(3) [3]=> int(4) [4]=> int(5) [0]=> int(1) [1]=> int(2) }
Output for 7.0.0 - 7.0.33
Parse error: syntax error, unexpected ')', expecting :: (T_PAAMAYIM_NEKUDOTAYIM) in /in/0Np90 on line 16
Process exited with code 255.
Output for 5.6.0 - 5.6.40
Parse error: syntax error, unexpected '[' in /in/0Np90 on line 16
Process exited with code 255.

preferences:
104.92 ms | 1949 KiB | 4 Q