3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php // Everything you enter here will be executed by our servers. Try it! function iterator_compose() { foreach (array_reverse(func_get_args()) as $it) { if (is_callable($it)) { $prev = $it; } } } function iterator_chunk(\Traversable $it, $chunkSize) { $it = new \IteratorIterator($it); $chunk = []; $steps = range(0, $chunkSize); while (true) { foreach ($steps as $_) { $chunk[] = $it->current(); $it->next(); if (!$it->valid()) { break 2; } } yield $chunk; $chunk = []; } if ($chunk) { yield $chunk; } } $it = new ArrayIterator(range(0, 125)); foreach (iterator_chunk($it, 32) as $i => $c) { echo "====== Chunk $i ======\n"; var_export($c); echo "\n"; }
Output for 5.5.0 - 5.6.28, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0
====== Chunk 0 ====== array ( 0 => NULL, 1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5, 6 => 6, 7 => 7, 8 => 8, 9 => 9, 10 => 10, 11 => 11, 12 => 12, 13 => 13, 14 => 14, 15 => 15, 16 => 16, 17 => 17, 18 => 18, 19 => 19, 20 => 20, 21 => 21, 22 => 22, 23 => 23, 24 => 24, 25 => 25, 26 => 26, 27 => 27, 28 => 28, 29 => 29, 30 => 30, 31 => 31, 32 => 32, ) ====== Chunk 1 ====== array ( 0 => 33, 1 => 34, 2 => 35, 3 => 36, 4 => 37, 5 => 38, 6 => 39, 7 => 40, 8 => 41, 9 => 42, 10 => 43, 11 => 44, 12 => 45, 13 => 46, 14 => 47, 15 => 48, 16 => 49, 17 => 50, 18 => 51, 19 => 52, 20 => 53, 21 => 54, 22 => 55, 23 => 56, 24 => 57, 25 => 58, 26 => 59, 27 => 60, 28 => 61, 29 => 62, 30 => 63, 31 => 64, 32 => 65, ) ====== Chunk 2 ====== array ( 0 => 66, 1 => 67, 2 => 68, 3 => 69, 4 => 70, 5 => 71, 6 => 72, 7 => 73, 8 => 74, 9 => 75, 10 => 76, 11 => 77, 12 => 78, 13 => 79, 14 => 80, 15 => 81, 16 => 82, 17 => 83, 18 => 84, 19 => 85, 20 => 86, 21 => 87, 22 => 88, 23 => 89, 24 => 90, 25 => 91, 26 => 92, 27 => 93, 28 => 94, 29 => 95, 30 => 96, 31 => 97, 32 => 98, ) ====== Chunk 3 ====== array ( 0 => 99, 1 => 100, 2 => 101, 3 => 102, 4 => 103, 5 => 104, 6 => 105, 7 => 106, 8 => 107, 9 => 108, 10 => 109, 11 => 110, 12 => 111, 13 => 112, 14 => 113, 15 => 114, 16 => 115, 17 => 116, 18 => 117, 19 => 118, 20 => 119, 21 => 120, 22 => 121, 23 => 122, 24 => 123, 25 => 124, 26 => 125, )
Output for 5.4.0 - 5.4.45
Parse error: syntax error, unexpected '$chunk' (T_VARIABLE) in /in/HkCtY on line 30
Process exited with code 255.
Output for 5.3.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/HkCtY on line 16
Process exited with code 255.
Output for 5.1.0 - 5.2.17
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/HkCtY on line 13 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/HkCtY on line 15 Parse error: syntax error, unexpected '[' in /in/HkCtY on line 16
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/HkCtY on line 13 Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/HkCtY on line 15 Parse error: parse error, unexpected '[' in /in/HkCtY on line 16
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/HkCtY on line 13 Parse error: syntax error, unexpected T_STRING, expecting ')' in /in/HkCtY on line 13
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/HkCtY on line 13 Parse error: parse error, unexpected T_STRING, expecting ')' in /in/HkCtY on line 13
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/HkCtY on line 13 Parse error: parse error, expecting `')'' in /in/HkCtY on line 13
Process exited with code 255.