3v4l.org

run code in 300+ PHP versions simultaneously
<?php class myIterator implements Iterator { private $position = 0; private $array = array( "firstelement_Iterator", "secondelement_Iterator", "lastelement_Iterator", ); public function __construct() { $this->position = 0; } function rewind() { $this->position = 0; } function current() { return $this->array[$this->position]; } function key() { return $this->position; } function next() { ++$this->position; } function valid() { return isset($this->array[$this->position]); } } $tests = []; $test0 = [ "firstelement_Iterator", "secondelement_Iterator", "lastelement_Iterator", ]; $test1 = new ArrayIterator([ "firstelement_ArrayIterator", "secondelement_ArrayIterator", "lastelement_ArrayIterator", ]); $test2 = new myIterator(); $tests = [ 'array' => $test0, 'array_iterator' => $test1, 'Iterator' => $test2, ]; foreach ($tests as $testName => $test) { echo "start test $testName\n"; $current = current($test); $i = 0; $current = current($test); foreach ($test as $key => $value) { //echo "$key, $value \n"; } //current returns the value of the array element that's currently being pointed to by the // internal pointer. // If the internal pointer points beyond the end of the elements list or the array is empty, current() returns FALSE. var_dump(current($test)); // reset() rewinds array's internal pointer to the first element and // returns the value of the first array element - this should be firstelement_* var_dump(reset($test)); echo "End test.\n\n"; }
Output for 8.1.0 - 8.1.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.7
Deprecated: Return type of myIterator::current() should either be compatible with Iterator::current(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/OmfJG on line 22 Deprecated: Return type of myIterator::next() should either be compatible with Iterator::next(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/OmfJG on line 30 Deprecated: Return type of myIterator::key() should either be compatible with Iterator::key(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/OmfJG on line 26 Deprecated: Return type of myIterator::valid() should either be compatible with Iterator::valid(): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/OmfJG on line 34 Deprecated: Return type of myIterator::rewind() should either be compatible with Iterator::rewind(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/OmfJG on line 18 start test array string(21) "firstelement_Iterator" string(21) "firstelement_Iterator" End test. start test array_iterator Deprecated: current(): Calling current() on an object is deprecated in /in/OmfJG on line 68 Deprecated: current(): Calling current() on an object is deprecated in /in/OmfJG on line 71 Deprecated: current(): Calling current() on an object is deprecated in /in/OmfJG on line 79 bool(false) Deprecated: reset(): Calling reset() on an object is deprecated in /in/OmfJG on line 82 bool(false) End test. start test Iterator Deprecated: current(): Calling current() on an object is deprecated in /in/OmfJG on line 68 Deprecated: current(): Calling current() on an object is deprecated in /in/OmfJG on line 71 Deprecated: current(): Calling current() on an object is deprecated in /in/OmfJG on line 79 int(3) Deprecated: reset(): Calling reset() on an object is deprecated in /in/OmfJG on line 82 int(3) End test.
Output for 7.4.0 - 7.4.33, 8.0.0 - 8.0.30
start test array string(21) "firstelement_Iterator" string(21) "firstelement_Iterator" End test. start test array_iterator bool(false) bool(false) End test. start test Iterator int(3) int(3) End test.
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.20, 7.2.0 - 7.2.33, 7.3.16 - 7.3.33
start test array string(21) "firstelement_Iterator" string(21) "firstelement_Iterator" End test. start test array_iterator string(26) "firstelement_ArrayIterator" string(26) "firstelement_ArrayIterator" End test. start test Iterator int(3) int(3) End test.
Output for 5.5.24 - 5.5.35, 5.6.8 - 5.6.28
start test array bool(false) string(21) "firstelement_Iterator" End test. start test array_iterator string(26) "firstelement_ArrayIterator" string(26) "firstelement_ArrayIterator" End test. start test Iterator int(3) int(3) End test.

preferences:
174.78 ms | 402 KiB | 184 Q