3v4l.org

run code in 300+ PHP versions simultaneously
<?php class MyIter implements Iterator { private $a; private $key = 0; public function __construct(array $a) { $this->a = array_values($a); } public function key() { return $this->key; } public function rewind() { $this->key = 0; } public function valid() { echo __METHOD__, PHP_EOL; return isset($this->a[$this->key]); } public function next() { echo __METHOD__, PHP_EOL; $this->key++; } public function current() { echo __METHOD__, PHP_EOL; return $this->a[$this->key]; } } $ilist = new MyIter(array('A', 'B', 'C', 'D')); foreach ($ilist as $var) { echo "var = $var\n"; echo "current = ", print_r(current($ilist), true), "\n"; }
Output for 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
Deprecated: Return type of MyIter::current() should either be compatible with Iterator::current(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/a7NtE on line 35 Deprecated: Return type of MyIter::next() should either be compatible with Iterator::next(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/a7NtE on line 29 Deprecated: Return type of MyIter::key() should either be compatible with Iterator::key(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/a7NtE on line 13 Deprecated: Return type of MyIter::valid() should either be compatible with Iterator::valid(): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/a7NtE on line 23 Deprecated: Return type of MyIter::rewind() should either be compatible with Iterator::rewind(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/a7NtE on line 18 MyIter::valid MyIter::current var = A current = Deprecated: current(): Calling current() on an object is deprecated in /in/a7NtE on line 45 Array ( [0] => A [1] => B [2] => C [3] => D ) MyIter::next MyIter::valid MyIter::current var = B current = Deprecated: current(): Calling current() on an object is deprecated in /in/a7NtE on line 45 Array ( [0] => A [1] => B [2] => C [3] => D ) MyIter::next MyIter::valid MyIter::current var = C current = Deprecated: current(): Calling current() on an object is deprecated in /in/a7NtE on line 45 Array ( [0] => A [1] => B [2] => C [3] => D ) MyIter::next MyIter::valid MyIter::current var = D current = Deprecated: current(): Calling current() on an object is deprecated in /in/a7NtE on line 45 Array ( [0] => A [1] => B [2] => C [3] => D ) MyIter::next MyIter::valid
Output for 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 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
MyIter::valid MyIter::current var = A current = Array ( [0] => A [1] => B [2] => C [3] => D ) MyIter::next MyIter::valid MyIter::current var = B current = Array ( [0] => A [1] => B [2] => C [3] => D ) MyIter::next MyIter::valid MyIter::current var = C current = Array ( [0] => A [1] => B [2] => C [3] => D ) MyIter::next MyIter::valid MyIter::current var = D current = Array ( [0] => A [1] => B [2] => C [3] => D ) MyIter::next MyIter::valid
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_ARRAY, expecting '&' or T_VARIABLE in /in/a7NtE on line 8
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting '{' in /in/a7NtE on line 3
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1
Parse error: parse error, unexpected T_STRING, expecting '{' in /in/a7NtE on line 3
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `'{'' in /in/a7NtE on line 3
Process exited with code 255.

preferences:
320.88 ms | 401 KiB | 460 Q