3v4l.org

run code in 300+ PHP versions simultaneously
<?php function coroutine1() { for ($i = 1; $i <= 5; $i++) { echo "coroutine1: {$i}\n"; yield; } } function coroutine2() { for ($i = 1; $i <= 10; $i++) { echo "coroutine2: {$i}\n"; $loop = yield; if ($i == 3) { echo "coroutine2 spawning coroutine3\n"; $loop->enqueue("#3", coroutine3()); } } } function coroutine3() { for ($i = 1; $i <= 5; $i++) { echo "coroutine3: {$i}\n"; yield; } } class EventLoop { private $tasks = []; public function enqueue($key, Generator $task) { $this->tasks[$key] = $task; } public function run() { foreach ($this->tasks as $key => $task) { $task->send($this); if (!$task->valid()) { echo "task {$key} complete\n"; unset($this->tasks[$key]); } } return !empty($this->tasks); } } $loop = new EventLoop(); $loop->enqueue("#1", coroutine1()); $loop->enqueue("#2", coroutine2()); while ($loop->run());
Output for git.master, git.master_jit, rfc.property-hooks
coroutine1: 1 coroutine1: 2 coroutine2: 1 coroutine2: 2 coroutine1: 3 coroutine2: 3 coroutine1: 4 coroutine2 spawning coroutine3 coroutine2: 4 coroutine1: 5 coroutine2: 5 coroutine3: 1 coroutine3: 2 task #1 complete coroutine2: 6 coroutine3: 3 coroutine2: 7 coroutine3: 4 coroutine2: 8 coroutine3: 5 coroutine2: 9 task #3 complete coroutine2: 10 task #2 complete

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:
49.4 ms | 402 KiB | 8 Q