3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php class interpreter { private $_code; public function __construct($code) { if(!is_string($code)) exit('Error while parsing code; Code not conformal.'); $this->_code = $code; $this->parser(); } public function parser($code = null) { if($code === null) $code = $this->_code; $calls = []; $variables = []; $conditions = []; preg_match('#(.*)\((.*)\)#', $code, $calls); preg_match('#if\((.*)\) \{(.*)\}#s', $code, $conditions); if($conditions && isset($conditions[0])) $this->conditions_ifelse($conditions); if($calls && isset($calls[0])) $this->calls($calls); } private function calls(array $calls) { switch($calls[1]) { case 'print': $this->func_print($calls[2]); break; } } private function condition_ifelse(array $conditions) { if(!preg_match('#(.*)(==|===|\<|\<=|\>|\>=|!=|!==|&&|\|\||AND|OR|XOR)(.*)#i', $conditions[1])) exit('Condition parsing error.'); } } abstract class lang { const version = 0.1; } class console extends lang { } class out extends console { public function println($arg) { if(preg_match('#^("|\')(.*)("|\')$#', $arg)) $arg = preg_replace('#^("|\')(.*)("|\')$#', '$2', $arg); echo($arg); } } $code = <<<CODE int var = 1; if(var == 1) { console.out.println("Hello World!"); } else { console.out.println("Hi!"); } CODE; $program = new interpreter($code);
based on 1B13W
Output for 7.0.0 - 7.3.0alpha1
Fatal error: Uncaught Error: Call to undefined method interpreter::conditions_ifelse() in /in/uth5B:16 Stack trace: #0 /in/uth5B(7): interpreter->parser() #1 /in/uth5B(49): interpreter->__construct('int var = 1;\nif...') #2 {main} thrown in /in/uth5B on line 16
Process exited with code 255.
Output for 5.4.11 - 5.6.28
Fatal error: Call to undefined method interpreter::conditions_ifelse() in /in/uth5B on line 16
Process exited with code 255.
Output for 5.3.21 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/uth5B on line 11
Process exited with code 255.