3v4l.org

run code in 300+ PHP versions simultaneously
<?php class VariableStream { var $position; var $varname; function _stream_open($path, $mode, $options, &$opened_path) { $url = parse_url($path); $this->varname = $url["host"]; $this->position = 0; return true; } function _stream_read($count) { $ret = substr($GLOBALS[$this->varname], $this->position, $count); $this->position += strlen($ret); return $ret; } function _stream_write($data) { $left = substr($GLOBALS[$this->varname], 0, $this->position); $right = substr($GLOBALS[$this->varname], $this->position + strlen($data)); $GLOBALS[$this->varname] = $left . $data . $right; $this->position += strlen($data); return strlen($data); } function _stream_tell() { return $this->position; } function _stream_eof() { return $this->position >= strlen($GLOBALS[$this->varname]); } function _stream_seek($offset, $whence) { switch ($whence) { case SEEK_SET: if ($offset < strlen($GLOBALS[$this->varname]) && $offset >= 0) { $this->position = $offset; return true; } else { return false; } break; case SEEK_CUR: if ($offset >= 0) { $this->position += $offset; return true; } else { return false; } break; case SEEK_END: if (strlen($GLOBALS[$this->varname]) + $offset >= 0) { $this->position = strlen($GLOBALS[$this->varname]) + $offset; return true; } else { return false; } break; default: return false; } } function _stream_metadata($path, $option, $var) { if($option == STREAM_META_TOUCH) { $url = parse_url($path); $varname = $url["host"]; if(!isset($GLOBALS[$varname])) { $GLOBALS[$varname] = ''; } return true; } return false; } public function __call($name, $arguments) { $name = '_' . $name; if (!method_exists($this, $name)) { return false; } return $this->$name(...$arguments); } } stream_wrapper_register("var", "VariableStream") or die("Failed to register protocol"); $myvar = ""; $fp = fopen("var://myvar", "r+"); fwrite($fp, "line1\n"); fwrite($fp, "line2\n"); fwrite($fp, "line3\n"); rewind($fp); while (!feof($fp)) { echo fgets($fp); } fclose($fp); var_dump($myvar);
Output for 8.2.0 - 8.2.19, 8.3.0 - 8.3.4, 8.3.6 - 8.3.7
Deprecated: Creation of dynamic property VariableStream::$context is deprecated in /in/QBBel on line 105 line1 line2 line3 string(18) "line1 line2 line3 "
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: Creation of dynamic property VariableStream::$context is deprecated in /in/QBBel on line 105 line1 line2 line3 string(18) "line1 line2 line3 "
Output for 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28
line1 line2 line3 string(18) "line1 line2 line3 "

preferences:
177.08 ms | 402 KiB | 154 Q