3v4l.org

run code in 300+ PHP versions simultaneously
<?php class InfiniteLoggingIterator implements Iterator { public function __construct() { $this->position = 0; } public function rewind() { echo __METHOD__, PHP_EOL; $this->position = 0; } public function current() { echo __METHOD__, PHP_EOL; return $this->position; } public function key() { echo __METHOD__, PHP_EOL; return $this->position; } public function next() { echo __METHOD__, PHP_EOL; ++$this->position; } public function valid() { echo __METHOD__, PHP_EOL; return true; } } $i = new LimitIterator(new InfiniteLoggingIterator, 0, 2); echo "Rewinding outer iterator\n"; $i->rewind(); echo "\nGetting 1st value\n"; echo $i->valid() ? "Valid\n" : "Not valid\n"; echo "Current key: ", $i->key() ?? 'NULL', "\n"; echo "Current value: ", $i->current() ?? 'NULL', "\n"; echo "Proceeding to 2nd value\n"; $i->next(); echo "\nGetting 2nd (and last) value\n"; echo $i->valid() ? "Valid\n" : "Not valid\n"; echo "Current key: ", $i->key() ?? 'NULL', "\n"; echo "Current value: ", $i->current() ?? 'NULL', "\n"; echo "Proceeding to 3rd value\n"; $i->next(); echo "\nGetting 3rd value (beyond limit)\n"; echo $i->valid() ? "Valid\n" : "Not valid\n"; echo "Current key: ", $i->key() ?? 'NULL', "\n"; echo "Current value: ", $i->current() ?? 'NULL', "\n"; echo "Proceeding to 4th value!\n"; $i->next();
Output for 8.1rc2
Deprecated: Return type of InfiniteLoggingIterator::current() should either be compatible with Iterator::current(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/IrAWU on line 13 Deprecated: Return type of InfiniteLoggingIterator::next() should either be compatible with Iterator::next(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/IrAWU on line 23 Deprecated: Return type of InfiniteLoggingIterator::key() should either be compatible with Iterator::key(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/IrAWU on line 18 Deprecated: Return type of InfiniteLoggingIterator::valid() should either be compatible with Iterator::valid(): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/IrAWU on line 28 Deprecated: Return type of InfiniteLoggingIterator::rewind() should either be compatible with Iterator::rewind(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/IrAWU on line 8 Rewinding outer iterator InfiniteLoggingIterator::rewind InfiniteLoggingIterator::valid InfiniteLoggingIterator::valid InfiniteLoggingIterator::current InfiniteLoggingIterator::key Getting 1st value Valid Current key: 0 Current value: 0 Proceeding to 2nd value InfiniteLoggingIterator::next InfiniteLoggingIterator::valid InfiniteLoggingIterator::current InfiniteLoggingIterator::key Getting 2nd (and last) value Valid Current key: 1 Current value: 1 Proceeding to 3rd value InfiniteLoggingIterator::next Getting 3rd value (beyond limit) Not valid Current key: NULL Current value: NULL Proceeding to 4th value! InfiniteLoggingIterator::next
Output for 8.1rc1
Deprecated: Return type of InfiniteLoggingIterator::current() should either be compatible with Iterator::current(): mixed, or the #[ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/IrAWU on line 13 Deprecated: Return type of InfiniteLoggingIterator::next() should either be compatible with Iterator::next(): void, or the #[ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/IrAWU on line 23 Deprecated: Return type of InfiniteLoggingIterator::key() should either be compatible with Iterator::key(): mixed, or the #[ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/IrAWU on line 18 Deprecated: Return type of InfiniteLoggingIterator::valid() should either be compatible with Iterator::valid(): bool, or the #[ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/IrAWU on line 28 Deprecated: Return type of InfiniteLoggingIterator::rewind() should either be compatible with Iterator::rewind(): void, or the #[ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/IrAWU on line 8 Rewinding outer iterator InfiniteLoggingIterator::rewind InfiniteLoggingIterator::valid InfiniteLoggingIterator::valid InfiniteLoggingIterator::current InfiniteLoggingIterator::key Getting 1st value Valid Current key: 0 Current value: 0 Proceeding to 2nd value InfiniteLoggingIterator::next InfiniteLoggingIterator::valid InfiniteLoggingIterator::current InfiniteLoggingIterator::key Getting 2nd (and last) value Valid Current key: 1 Current value: 1 Proceeding to 3rd value InfiniteLoggingIterator::next Getting 3rd value (beyond limit) Not valid Current key: NULL Current value: NULL Proceeding to 4th value! InfiniteLoggingIterator::next
Output for 7.3.0 - 7.3.30, 7.4.0 - 7.4.23, 8.0.0 - 8.0.11
Rewinding outer iterator InfiniteLoggingIterator::rewind InfiniteLoggingIterator::valid InfiniteLoggingIterator::valid InfiniteLoggingIterator::current InfiniteLoggingIterator::key Getting 1st value Valid Current key: 0 Current value: 0 Proceeding to 2nd value InfiniteLoggingIterator::next InfiniteLoggingIterator::valid InfiniteLoggingIterator::current InfiniteLoggingIterator::key Getting 2nd (and last) value Valid Current key: 1 Current value: 1 Proceeding to 3rd value InfiniteLoggingIterator::next Getting 3rd value (beyond limit) Not valid Current key: NULL Current value: NULL Proceeding to 4th value! InfiniteLoggingIterator::next
Output for 5.6.40
Parse error: syntax error, unexpected '?' in /in/IrAWU on line 41
Process exited with code 255.

preferences:
48.91 ms | 466 KiB | 5 Q