3v4l.org

run code in 500+ PHP versions simultaneously
<?php function debug($msg){ fwrite(STDOUT, sprintf("[Debug] %s\n", $msg)); } // capture any unexpected output and exit execution ob_start(function($buffer, $phase){ // prevent exit on ob_end_*() if(($phase & PHP_OUTPUT_HANDLER_FINAL) === 0){ $f = new class ($buffer) { public function __construct(private $buffer) {} public function __destruct() { debug('exit call'); exit(); // or // throw new UnexpectedOutputException($this->buffer); } }; } debug(sprintf("callback called, buffer: %s, type: %d", var_export($buffer, true), $phase)); // prevent buffer call return null; }, 1); // do some work on shutdown register_shutdown_function(function() { debug('ob_end_clean'); ob_end_clean(); debug('shutting down'); }); debug('echo x1'); echo 'x1'; debug('echo x2'); echo 'x2';
Output for 7.4.0 - 7.4.33
Parse error: syntax error, unexpected 'private' (T_PRIVATE), expecting variable (T_VARIABLE) in /in/ZNr6L on line 12
Process exited with code 255.
Output for 8.0.1 - 8.0.30, 8.1.0 - 8.1.34, 8.2.0 - 8.2.34, 8.3.0 - 8.3.35, 8.4.1 - 8.4.26, 8.5.0 - 8.5.11
[Debug] echo x1 [Debug] callback called, buffer: 'x1', type: 1 [Debug] exit call [Debug] ob_end_clean [Debug] callback called, buffer: '', type: 10 [Debug] shutting down

preferences:
31.74 ms | 1140 KiB | 9 Q