3v4l.org

run code in 300+ PHP versions simultaneously
<?php define("TYPE", "CLI"); abstract class View { protected $imp; public function __construct() { if (TYPE == 'CLI') $this->imp = new ViewImplCli(); else if (TYPE == 'JSON') $this->imp = new ViewImplJson(); else throw new Exception('Unknown environment'); } public function addText($text) { return $this->imp->addText($text); } public function addLine() { return $this->imp->addLine(); } public function getResult() { print $this->imp->getResult(); } } class ViewContent extends View { public function printParagraph($text) { $this->addText($text); } } class ViewTable extends View { public function addCell($text) { $this->addLine(); $this->addText($text); $this->addLine(); } } abstract class ViewImpl { abstract public function addText($text); abstract public function addLine(); abstract public function getResult(); } class ViewImplCli extends ViewImpl { protected $result = ""; public function addLine() { $this->result .= str_repeat('-', 80).PHP_EOL; } public function addText($text) { $this->result .= $text.PHP_EOL; } public function getResult() { return $this->result; } } class ViewImplJson extends ViewImpl { protected $result = array(); public function addLine() { $this->result[] = array('type' => 'line'); } public function addText($text) { $this->result[] = array('type' => 'text', 'text' => $text); } public function getResult() { return json_encode($this->result); } } $content = new ViewContent(); $content->printParagraph('Hello world'); echo $content->getResult(); $table = new ViewTable(); $table->addCell('Hello world in cell'); echo $table->getResult();
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
Hello world -------------------------------------------------------------------------------- Hello world in cell --------------------------------------------------------------------------------
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 Hello world -------------------------------------------------------------------------------- Hello world in cell --------------------------------------------------------------------------------

preferences:
179.51 ms | 402 KiB | 215 Q