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 git.master, git.master_jit, rfc.property-hooks
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.

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
55.5 ms | 401 KiB | 8 Q