3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Connection { public function query() { return new Result(); } } class Result implements \IteratorAggregate { private $rows; public function __construct() { $this->rows = new \ArrayObject([ [0, 1, 2], [3, 4, 5], [6, 7, 8], ]); } public function getIterator() { return $this->rows->getIterator(); } } $conn = new Connection(); // Here $result holds an instance of "Result" // *** THIS IS YOUR CASE 2 *** $result = $conn->query(); var_dump($result); // "Result" is iterable, so we can do like this foreach($result as $row) { var_dump($row); } // Or we can skip the intermediate variable and loop over the result immediately. // *** THIS IS YOUR CASE 1 *** foreach($conn->query() as $row) { var_dump($row); }
Output for 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
Deprecated: Return type of Result::getIterator() should either be compatible with IteratorAggregate::getIterator(): Traversable, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/8ekfh on line 24 object(Result)#2 (1) { ["rows":"Result":private]=> object(ArrayObject)#3 (1) { ["storage":"ArrayObject":private]=> array(3) { [0]=> array(3) { [0]=> int(0) [1]=> int(1) [2]=> int(2) } [1]=> array(3) { [0]=> int(3) [1]=> int(4) [2]=> int(5) } [2]=> array(3) { [0]=> int(6) [1]=> int(7) [2]=> int(8) } } } } array(3) { [0]=> int(0) [1]=> int(1) [2]=> int(2) } array(3) { [0]=> int(3) [1]=> int(4) [2]=> int(5) } array(3) { [0]=> int(6) [1]=> int(7) [2]=> int(8) } array(3) { [0]=> int(0) [1]=> int(1) [2]=> int(2) } array(3) { [0]=> int(3) [1]=> int(4) [2]=> int(5) } array(3) { [0]=> int(6) [1]=> int(7) [2]=> int(8) }
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 Deprecated: Return type of Result::getIterator() should either be compatible with IteratorAggregate::getIterator(): Traversable, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/8ekfh on line 24 object(Result)#2 (1) { ["rows":"Result":private]=> object(ArrayObject)#3 (1) { ["storage":"ArrayObject":private]=> array(3) { [0]=> array(3) { [0]=> int(0) [1]=> int(1) [2]=> int(2) } [1]=> array(3) { [0]=> int(3) [1]=> int(4) [2]=> int(5) } [2]=> array(3) { [0]=> int(6) [1]=> int(7) [2]=> int(8) } } } } array(3) { [0]=> int(0) [1]=> int(1) [2]=> int(2) } array(3) { [0]=> int(3) [1]=> int(4) [2]=> int(5) } array(3) { [0]=> int(6) [1]=> int(7) [2]=> int(8) } array(3) { [0]=> int(0) [1]=> int(1) [2]=> int(2) } array(3) { [0]=> int(3) [1]=> int(4) [2]=> int(5) } array(3) { [0]=> int(6) [1]=> int(7) [2]=> int(8) }
Output for 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
object(Result)#2 (1) { ["rows":"Result":private]=> object(ArrayObject)#3 (1) { ["storage":"ArrayObject":private]=> array(3) { [0]=> array(3) { [0]=> int(0) [1]=> int(1) [2]=> int(2) } [1]=> array(3) { [0]=> int(3) [1]=> int(4) [2]=> int(5) } [2]=> array(3) { [0]=> int(6) [1]=> int(7) [2]=> int(8) } } } } array(3) { [0]=> int(0) [1]=> int(1) [2]=> int(2) } array(3) { [0]=> int(3) [1]=> int(4) [2]=> int(5) } array(3) { [0]=> int(6) [1]=> int(7) [2]=> int(8) } array(3) { [0]=> int(0) [1]=> int(1) [2]=> int(2) } array(3) { [0]=> int(3) [1]=> int(4) [2]=> int(5) } array(3) { [0]=> int(6) [1]=> int(7) [2]=> int(8) }

preferences:
277.7 ms | 405 KiB | 291 Q