3v4l.org

run code in 300+ PHP versions simultaneously
<?php function generator_to_array(\Generator $generator) { $yielded = iterator_to_array($generator); return [ 'yielded' => $yielded, 'returned' => $generator->getReturn(), ]; } class test { private $whatever = false; public function __construct() { $a = generator_to_array($this->firstIterator()); $b = generator_to_array($this->secondIterator($this->firstIterator())); $c = generator_to_array($this->thirdIterator($this->firstIterator())); $this->whatever = true; $d = generator_to_array($this->firstIterator()); $e = generator_to_array($this->secondIterator($this->firstIterator())); $f = generator_to_array($this->thirdIterator($this->firstIterator())); var_dump([$a, $b, $c, $d, $e, $f]); } public function firstIterator(): \Iterator { yield 'a'=> 1; yield 'b'=> 2; yield 'c'=> 3; } public function secondIterator(\Iterator $first): \Iterator { if ($this->whatever === true) { return $first; } foreach($first as $key=>$value) { yield $key => $value * $value; } } public function thirdIterator(\Iterator $first): \Iterator { if ($this->whatever === true) { return $first; } return $this->thirdSubIterator($first); } public function thirdSubIterator(\Iterator $first): \Iterator { foreach($first as $key=>$value) { yield $key => $value * $value; } } } new test();
Output for git.master, git.master_jit, rfc.property-hooks
array(6) { [0]=> array(2) { ["yielded"]=> array(3) { ["a"]=> int(1) ["b"]=> int(2) ["c"]=> int(3) } ["returned"]=> NULL } [1]=> array(2) { ["yielded"]=> array(3) { ["a"]=> int(1) ["b"]=> int(4) ["c"]=> int(9) } ["returned"]=> NULL } [2]=> array(2) { ["yielded"]=> array(3) { ["a"]=> int(1) ["b"]=> int(4) ["c"]=> int(9) } ["returned"]=> NULL } [3]=> array(2) { ["yielded"]=> array(3) { ["a"]=> int(1) ["b"]=> int(2) ["c"]=> int(3) } ["returned"]=> NULL } [4]=> array(2) { ["yielded"]=> array(0) { } ["returned"]=> object(Generator)#4 (0) { } } [5]=> array(2) { ["yielded"]=> array(3) { ["a"]=> int(1) ["b"]=> int(2) ["c"]=> int(3) } ["returned"]=> NULL } }

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
59.44 ms | 403 KiB | 8 Q