3v4l.org

run code in 300+ PHP versions simultaneously
<?php function generator(): iterable { $count = -1; while (true) { echo 'Stopping: ', $count, \PHP_EOL; yield ++$count; echo 'Resuming: ', $count, \PHP_EOL; } } function limit(\Iterator $inner, int $count) { if (1 > $count) return; $inner->rewind(); while ($inner->valid()) { if (!$count--) { return; } yield $inner->key() => $inner->current(); $inner->next(); } } echo "Construct:\n"; $i = limit(generator(), 3); echo "Rewind:\n"; $i->rewind(); echo "\n1st iteration\n"; echo $i->valid() ? "Valid\n" : "Not valid\n"; echo "Current value: ", $i->current(), "\n"; $i->next(); echo "\n2nd iteration\n"; echo $i->valid() ? "Valid\n" : "Not valid\n"; echo "Current value: ", $i->current(), "\n"; $i->next(); echo "\n3rd iteration\n"; echo $i->valid() ? "Valid\n" : "Not valid\n"; echo "Current value: ", $i->current(), "\n"; $i->next(); echo "\n4th iteration\n"; echo $i->valid() ? "Valid\n" : "Not valid\n"; echo "Current value: ", $i->current(), "\n"; $i->next(); echo "\n5th iteration\n"; echo $i->valid() ? "Valid\n" : "Not valid\n"; echo "Current value: ", $i->current(), "\n"; $i->next();
Output for 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.4, 8.3.6
Construct: Rewind: Stopping: -1 1st iteration Valid Current value: 0 Resuming: 0 Stopping: 0 2nd iteration Valid Current value: 1 Resuming: 1 Stopping: 1 3rd iteration Valid Current value: 2 Resuming: 2 Stopping: 2 4th iteration Not valid Current value: 5th iteration Not valid Current value:
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 Construct: Rewind: Stopping: -1 1st iteration Valid Current value: 0 Resuming: 0 Stopping: 0 2nd iteration Valid Current value: 1 Resuming: 1 Stopping: 1 3rd iteration Valid Current value: 2 Resuming: 2 Stopping: 2 4th iteration Not valid Current value: 5th iteration Not valid Current value:

preferences:
151.74 ms | 402 KiB | 153 Q