3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Implements a read/write buffer. */ class Buffer { protected $eof = false; protected $indexRead = 0; protected $indexWrite = 0; protected $buffer = []; public function write($bytes) { if ($bytes === null) { $this->eof = true; } else { $this->buffer[$this->indexWrite++] = $bytes; } } public function read($length = 4096) { if (!$this->buffer) { return $this->eof ? null : ''; } $output = ''; $outputLength = 0; while ($this->indexRead < $this->indexWrite) { $chunk = $this->buffer[$this->indexRead]; $chunkLength = \strlen($chunk); if ($outputLength + $chunkLength == $length) { unset($this->buffer[$this->indexRead++]); $output .= $chunk; return $output; } if ($outputLength + $chunkLength > $length) { $slice = $length - $outputLength; $this->buffer[$this->indexRead] = \substr($chunk, $slice); $output .= \substr($chunk, 0, $slice); return $output; } unset($this->buffer[$this->indexRead++]); $output .= $chunk; $outputLength += $chunkLength; } return $output; } } $b = new Buffer(); $b->write('ABCDEFGHIJ'); $b->write('KLMNOPQRSTUVWXYZ'); var_dump($b->read(3)); var_dump($b->read(3)); var_dump($b->read(3)); var_dump($b->read(3)); var_dump($b->read(3)); var_dump($b->read(3)); var_dump($b->read(3)); var_dump($b->read(3)); var_dump($b->read(3)); var_dump($b->read(3)); var_dump($b->read(3)); var_dump($b->read(3)); var_dump($b->read(3)); var_dump($b->read(3)); var_dump($b->read(3)); var_dump($b->read(3)); var_dump($b->read(3)); echo 'Writing more...' . PHP_EOL; $b->write('01234567890'); var_dump($b->read(3)); var_dump($b->read(3)); var_dump($b->read(3)); var_dump($b->read(3)); var_dump($b->read(3)); echo 'EOF...' . PHP_EOL; $b->write(null); var_dump($b->read(3)); var_dump($b->read(3)); var_dump($b->read(3));
Output for git.master, git.master_jit, rfc.property-hooks
string(3) "ABC" string(3) "DEF" string(3) "GHI" string(3) "JKL" string(3) "MNO" string(3) "PQR" string(3) "STU" string(3) "VWX" string(2) "YZ" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" string(0) "" Writing more... string(3) "012" string(3) "345" string(3) "678" string(2) "90" string(0) "" EOF... NULL NULL 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:
39.4 ms | 405 KiB | 9 Q