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);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/q5Ic8
function name:  (null)
number of ops:  11
compiled vars:  !0 = $template, !1 = $data
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   DECLARE_CONST                                            'ERRMODE_EMPTY', 1
    4     1        DECLARE_CONST                                            'ERRMODE_LITERAL', 2
    5     2        DECLARE_CONST                                            'ERRMODE_EXCEPTION', 3
   39     3        ASSIGN                                                   !0, 'HELLO+%7Bname%7D%0AHow+are+you+doing+%7Bwhen%7D%0AFeeling+%40%7Bok%7D%3F'
   45     4        ASSIGN                                                   !1, <array>
   50     5        INIT_FCALL                                               'render_template'
          6        SEND_VAR                                                 !0
          7        SEND_VAR                                                 !1
          8        DO_FCALL                                      0  $4      
          9        ECHO                                                     $4
         10      > RETURN                                                   1

Function render_template:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/q5Ic8
function name:  render_template
number of ops:  29
compiled vars:  !0 = $template, !1 = $variables, !2 = $escapeChar, !3 = $errMode, !4 = $esc, !5 = $expr, !6 = $callback
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    7     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV_INIT                                        !2      '%40'
          3        RECV_INIT                                        !3      <const ast>
    8     4        INIT_FCALL                                               'preg_quote'
          5        SEND_VAR                                                 !2
          6        DO_ICALL                                         $7      
          7        ASSIGN                                                   !4, $7
    9     8        ROPE_INIT                                     8  ~10     '%2F%0A++++++++'
   10     9        ROPE_ADD                                      1  ~10     ~10, !4
         10        ROPE_ADD                                      2  ~10     ~10, !4
         11        ROPE_ADD                                      3  ~10     ~10, '%28%3F%3D'
         12        ROPE_ADD                                      4  ~10     ~10, !4
         13        ROPE_ADD                                      5  ~10     ~10, '%2A%2B%7B%29%0A++++++%7C+'
   11    14        ROPE_ADD                                      6  ~10     ~10, !4
         15        ROPE_END                                      7  ~9      ~10, '%7B%0A++++++%7C+%7B%28%5Cw%2B%29%7D%0A++++%2Fx'
    9    16        ASSIGN                                                   !5, ~9
   15    17        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2Fq5Ic8%3A15%240'
         18        BIND_LEXICAL                                             ~15, !1
         19        BIND_LEXICAL                                             ~15, !2
         20        BIND_LEXICAL                                             ~15, !3
         21        ASSIGN                                                   !6, ~15
   36    22        INIT_FCALL                                               'preg_replace_callback'
         23        SEND_VAR                                                 !5
         24        SEND_VAR                                                 !6
         25        SEND_VAR                                                 !0
         26        DO_ICALL                                         $17     
         27      > RETURN                                                   $17
   37    28*     > RETURN                                                   null

End of function render_template

Function %00%7Bclosure%7D%2Fin%2Fq5Ic8%3A15%240:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 44) Position 1 = 8, Position 2 = 12
Branch analysis from position: 8
2 jumps found. (Code = 44) Position 1 = 11, Position 2 = 14
Branch analysis from position: 11
1 jumps found. (Code = 42) Position 1 = 16
Branch analysis from position: 16
2 jumps found. (Code = 43) Position 1 = 19, Position 2 = 22
Branch analysis from position: 19
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 22
2 jumps found. (Code = 43) Position 1 = 25, Position 2 = 29
Branch analysis from position: 25
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 29
2 jumps found. (Code = 43) Position 1 = 32, Position 2 = 39
Branch analysis from position: 32
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 39
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/q5Ic8
function name:  {closure}
number of ops:  43
compiled vars:  !0 = $match, !1 = $variables, !2 = $escapeChar, !3 = $errMode
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   15     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
          2        BIND_STATIC                                              !2
          3        BIND_STATIC                                              !3
   16     4        FETCH_DIM_R                                      ~4      !0, 0
   17     5        CONCAT                                           ~6      !2, !2
          6        CASE                                                     ~4, ~6
          7      > JMPNZ                                                    ~5, ->12
   20     8    >   CONCAT                                           ~7      !2, '%7B'
          9        CASE                                                     ~4, ~7
         10      > JMPNZ                                                    ~5, ->14
         11    > > JMP                                                      ->16
   18    12    >   FREE                                                     ~4
         13      > RETURN                                                   !2
   21    14    >   FREE                                                     ~4
         15      > RETURN                                                   '%7B'
   24    16    >   FETCH_CONSTANT                                   ~8      'ERRMODE_EMPTY'
         17        IS_IDENTICAL                                             !3, ~8
         18      > JMPZ                                                     ~9, ->22
   25    19    >   FREE                                                     ~4
         20      > RETURN                                                   ''
         21*       JMP                                                      ->41
   26    22    >   FETCH_CONSTANT                                   ~10     'ERRMODE_LITERAL'
         23        IS_IDENTICAL                                             !3, ~10
         24      > JMPZ                                                     ~11, ->29
   27    25    >   FETCH_DIM_R                                      ~12     !0, 0
         26        FREE                                                     ~4
         27      > RETURN                                                   ~12
         28*       JMP                                                      ->41
   28    29    >   FETCH_CONSTANT                                   ~13     'ERRMODE_EXCEPTION'
         30        IS_IDENTICAL                                             !3, ~13
         31      > JMPZ                                                     ~14, ->39
   29    32    >   NEW                                              $15     'Exception'
         33        FETCH_DIM_R                                      ~16     !0, 1
         34        CONCAT                                           ~17     'Unknown+placeholder%3A+', ~16
         35        SEND_VAL_EX                                              ~17
         36        DO_FCALL                                      0          
         37      > THROW                                         0          $15
         38*       JMP                                                      ->41
   31    39    >   FREE                                                     ~4
         40      > RETURN                                                   !3
         41*       FREE                                                     ~4
   34    42*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2Fq5Ic8%3A15%240

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
172.65 ms | 1407 KiB | 18 Q