3v4l.org

run code in 300+ PHP versions simultaneously
<?php $txt = <<<TXT Line 1 Line 2 Line 3 Line 4 Line 5 Line 6 Line 7 Line 8 TXT; class FileObject extends SplFileObject { #[\ReturnTypeWillChange] public function seek($offset) { if ($offset < 0) { throw new Exception("Can't seek file: " . $this->getPathname() . " to negative offset: $offset"); } if ($offset === 0) { parent::seek(0); return; } $this->rewind(); for ($i = 0; $i < $offset; $i++) { $this->fgets(); $this->next(); if ($this->eof()) { break; } } //$this->current(); } } $file = new FileObject('php://memory', 'w+'); $file->fwrite($txt); echo 'Seeking line 2...', PHP_EOL; $file->seek(1); echo 'Position in the file: ', $file->ftell(), PHP_EOL; echo 'Rest of the file: ', PHP_EOL, $file->fread(5000); echo PHP_EOL, '-------------', PHP_EOL; echo 'Seeking line last line...', PHP_EOL; $file->seek(PHP_INT_MAX); $lastLine = $file->key(); $offset = max($lastLine - 1, 0); $file->seek($offset); echo 'Position in the file: ', $file->ftell(), PHP_EOL; echo 'Rest of the file: ', PHP_EOL, $file->fread(5000);
Output for 5.5.11 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.34, 7.3.0 - 7.3.33, 7.4.0 - 7.4.27, 8.0.0 - 8.0.14, 8.1.0 - 8.1.2
Seeking line 2... Position in the file: 7 Rest of the file: Line 2 Line 3 Line 4 Line 5 Line 6 Line 7 Line 8 ------------- Seeking line last line... Position in the file: 49 Rest of the file: Line 8
Output for 5.1.4 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.10
Seeking line 2... Position in the file: 7 Rest of the file: Fatal error: Call to undefined method FileObject::fread() in /in/Uhlpb on line 47
Process exited with code 255.
Output for 5.1.0 - 5.1.1, 5.1.3
Fatal error: Uncaught exception 'RuntimeException' with message 'SplFileObject::__construct(php://memory): failed to open stream: Success' in /in/Uhlpb:41 Stack trace: #0 /in/Uhlpb(41): SplFileObject->__construct('php://memory', 'w+') #1 {main} thrown in /in/Uhlpb on line 41
Process exited with code 255.
Output for 5.1.2
Fatal error: Uncaught exception 'RuntimeException' with message 'SplFileObject::__construct(php://memory): failed to open stream: Success' in /in/Uhlpb:42 Stack trace: #0 /in/Uhlpb(42): SplFileObject->__construct('php://memory', 'w+') #1 {main} thrown in /in/Uhlpb on line 42
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Fatal error: Class 'SplFileObject' not found in /in/Uhlpb on line 14
Process exited with code 255.
Output for 4.4.3 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/Uhlpb on line 17
Process exited with code 255.
Output for 4.4.2
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/Uhlpb on line 18
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/Uhlpb on line 17
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/Uhlpb on line 17
Process exited with code 255.

preferences:
59.57 ms | 471 KiB | 5 Q