3v4l.org

run code in 300+ PHP versions simultaneously
<?php const ERRMODE_EMPTY = 1; const ERRMODE_LITERAL = 2; const ERRMODE_EXCEPTION = 3; function render_template($template, array $variables, $escapeChar = '@', $errMode = ERRMODE_EXCEPTION) { $esc = preg_quote($escapeChar); $expr = "/ $esc$esc(?=$esc*+{) | $esc{ | {(\w+)} /x"; $callback = function($match) use($variables, $escapeChar, $errMode) { switch ($match[0]) { case $escapeChar . $escapeChar: return $escapeChar; case $escapeChar . '{': return '{'; default: if ($errMode === ERRMODE_EMPTY) { return ''; } else if ($errMode === ERRMODE_LITERAL) { return $match[0]; } else if ($errMode === ERRMODE_EXCEPTION) { throw new \Exception('Unknown placeholder: ' . $match[1]); } else { return $errMode; } } }; return preg_replace_callback($expr, $callback, $template); } $template = <<<'TPL' HELLO {name} How are you doing {when} Feeling @{ok}? TPL; $data = [ 'name' => 'WORLD', 'when' => 'yesterday', ]; echo render_template($template, $data);
Output for 7.0.0 - 7.0.33, 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.6
Fatal error: Uncaught Exception: Unknown placeholder: name in /in/q5Ic8:29 Stack trace: #0 [internal function]: {closure}(Array) #1 /in/q5Ic8(36): preg_replace_callback('/\n @@(?=...', Object(Closure), 'HELLO {name}\nHo...') #2 /in/q5Ic8(50): render_template('HELLO {name}\nHo...', Array) #3 {main} thrown in /in/q5Ic8 on line 29
Process exited with code 255.
Output for 5.4.27 - 5.4.45, 5.5.11 - 5.5.38, 5.6.0 - 5.6.40
Fatal error: Uncaught exception 'Exception' with message 'Unknown placeholder: name' in /in/q5Ic8:29 Stack trace: #0 [internal function]: {closure}(Array) #1 /in/q5Ic8(36): preg_replace_callback('/\n @@(?=...', Object(Closure), 'HELLO {name}\nHo...') #2 /in/q5Ic8(50): render_template('HELLO {name}\nHo...', Array) #3 {main} thrown in /in/q5Ic8 on line 29
Process exited with code 255.
Output for 5.4.0 - 5.4.26, 5.5.0 - 5.5.10
Fatal error: Uncaught exception 'Exception' with message 'Unknown placeholder: name' in /in/q5Ic8:29 Stack trace: #0 [internal function]: {closure}(Array) #1 /in/q5Ic8(36): preg_replace_callback('/? @@(?=...', Object(Closure), 'HELLO {name}?Ho...') #2 /in/q5Ic8(50): render_template('HELLO {name}?Ho...', Array) #3 {main} thrown in /in/q5Ic8 on line 29
Process exited with code 255.
Output for 5.3.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/q5Ic8 on line 45
Process exited with code 255.
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17
Parse error: syntax error, unexpected T_CONST in /in/q5Ic8 on line 3
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, 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_CONST in /in/q5Ic8 on line 3
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/q5Ic8 on line 3
Process exited with code 255.

preferences:
361.5 ms | 401 KiB | 459 Q