3v4l.org

run code in 300+ PHP versions simultaneously
<?php $source = json_encode([new StdClass, "This is a test", "Foo's bar", 1, 5]); function escapeForJs($string) { $len = mb_strlen($string, 'utf8'); $array = []; for ($i = 0; $i < $len; $i++) { $array[] = mb_substr($string, $i, 1, 'utf8'); } return implode("", array_map(function($char) { if (ctype_alnum($char)) { return $char; } $bytelen = mb_strlen($char, '8bit'); if ($bytelen === 1) { return '\\x' . str_pad(dechex(ord($char)), 2, '0', STR_PAD_LEFT); } $result = '\\u'; for ($i = 0; $i < $bytelen; $i++) { $result .= str_pad(dechex(ord($char[$i])), 2, '0', STR_PAD_LEFT); } return $result; }, $array)); } var_dump(escapeForJs($source)); var_dump(json_encode($source));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/leN7E
function name:  (null)
number of ops:  24
compiled vars:  !0 = $source
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   INIT_FCALL                                               'json_encode'
          1        NEW                                              $1      'StdClass'
          2        DO_FCALL                                      0          
          3        INIT_ARRAY                                       ~3      $1
          4        ADD_ARRAY_ELEMENT                                ~3      'This+is+a+test'
          5        ADD_ARRAY_ELEMENT                                ~3      'Foo%27s+bar'
          6        ADD_ARRAY_ELEMENT                                ~3      1
          7        ADD_ARRAY_ELEMENT                                ~3      5
          8        SEND_VAL                                                 ~3
          9        DO_ICALL                                         $4      
         10        ASSIGN                                                   !0, $4
   28    11        INIT_FCALL                                               'var_dump'
         12        INIT_FCALL                                               'escapeforjs'
         13        SEND_VAR                                                 !0
         14        DO_FCALL                                      0  $6      
         15        SEND_VAR                                                 $6
         16        DO_ICALL                                                 
   29    17        INIT_FCALL                                               'var_dump'
         18        INIT_FCALL                                               'json_encode'
         19        SEND_VAR                                                 !0
         20        DO_ICALL                                         $8      
         21        SEND_VAR                                                 $8
         22        DO_ICALL                                                 
         23      > RETURN                                                   1

Function escapeforjs:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 18
Branch analysis from position: 18
2 jumps found. (Code = 44) Position 1 = 20, Position 2 = 9
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
2 jumps found. (Code = 44) Position 1 = 20, Position 2 = 9
Branch analysis from position: 20
Branch analysis from position: 9
filename:       /in/leN7E
function name:  escapeForJs
number of ops:  31
compiled vars:  !0 = $string, !1 = $len, !2 = $array, !3 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    5     0  E >   RECV                                             !0      
    6     1        INIT_FCALL                                               'mb_strlen'
          2        SEND_VAR                                                 !0
          3        SEND_VAL                                                 'utf8'
          4        DO_ICALL                                         $4      
          5        ASSIGN                                                   !1, $4
    7     6        ASSIGN                                                   !2, <array>
    8     7        ASSIGN                                                   !3, 0
          8      > JMP                                                      ->18
    9     9    >   INIT_FCALL                                               'mb_substr'
         10        SEND_VAR                                                 !0
         11        SEND_VAR                                                 !3
         12        SEND_VAL                                                 1
         13        SEND_VAL                                                 'utf8'
         14        DO_ICALL                                         $9      
         15        ASSIGN_DIM                                               !2
         16        OP_DATA                                                  $9
    8    17        PRE_INC                                                  !3
         18    >   IS_SMALLER                                               !3, !1
         19      > JMPNZ                                                    ~11, ->9
   11    20    >   INIT_FCALL                                               'implode'
         21        SEND_VAL                                                 ''
         22        INIT_FCALL                                               'array_map'
         23        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FleN7E%3A11%240'
   25    24        SEND_VAL                                                 ~12
         25        SEND_VAR                                                 !2
         26        DO_ICALL                                         $13     
         27        SEND_VAR                                                 $13
         28        DO_ICALL                                         $14     
         29      > RETURN                                                   $14
   26    30*     > RETURN                                                   null

End of function escapeforjs

Function %00%7Bclosure%7D%2Fin%2FleN7E%3A11%240:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 6
Branch analysis from position: 5
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 27
Branch analysis from position: 13
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 27
1 jumps found. (Code = 42) Position 1 = 45
Branch analysis from position: 45
2 jumps found. (Code = 44) Position 1 = 47, Position 2 = 30
Branch analysis from position: 47
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 30
2 jumps found. (Code = 44) Position 1 = 47, Position 2 = 30
Branch analysis from position: 47
Branch analysis from position: 30
filename:       /in/leN7E
function name:  {closure}
number of ops:  49
compiled vars:  !0 = $char, !1 = $bytelen, !2 = $result, !3 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   11     0  E >   RECV                                             !0      
   12     1        INIT_FCALL                                               'ctype_alnum'
          2        SEND_VAR                                                 !0
          3        DO_ICALL                                         $4      
          4      > JMPZ                                                     $4, ->6
   13     5    > > RETURN                                                   !0
   15     6    >   INIT_FCALL                                               'mb_strlen'
          7        SEND_VAR                                                 !0
          8        SEND_VAL                                                 '8bit'
          9        DO_ICALL                                         $5      
         10        ASSIGN                                                   !1, $5
   17    11        IS_IDENTICAL                                             !1, 1
         12      > JMPZ                                                     ~7, ->27
   18    13    >   INIT_FCALL                                               'str_pad'
         14        INIT_FCALL                                               'dechex'
         15        INIT_FCALL                                               'ord'
         16        SEND_VAR                                                 !0
         17        DO_ICALL                                         $8      
         18        SEND_VAR                                                 $8
         19        DO_ICALL                                         $9      
         20        SEND_VAR                                                 $9
         21        SEND_VAL                                                 2
         22        SEND_VAL                                                 '0'
         23        SEND_VAL                                                 0
         24        DO_ICALL                                         $10     
         25        CONCAT                                           ~11     '%5Cx', $10
         26      > RETURN                                                   ~11
   20    27    >   ASSIGN                                                   !2, '%5Cu'
   21    28        ASSIGN                                                   !3, 0
         29      > JMP                                                      ->45
   22    30    >   INIT_FCALL                                               'str_pad'
         31        INIT_FCALL                                               'dechex'
         32        INIT_FCALL                                               'ord'
         33        FETCH_DIM_R                                      ~14     !0, !3
         34        SEND_VAL                                                 ~14
         35        DO_ICALL                                         $15     
         36        SEND_VAR                                                 $15
         37        DO_ICALL                                         $16     
         38        SEND_VAR                                                 $16
         39        SEND_VAL                                                 2
         40        SEND_VAL                                                 '0'
         41        SEND_VAL                                                 0
         42        DO_ICALL                                         $17     
         43        ASSIGN_OP                                     8          !2, $17
   21    44        PRE_INC                                                  !3
         45    >   IS_SMALLER                                               !3, !1
         46      > JMPNZ                                                    ~20, ->30
   24    47    > > RETURN                                                   !2
   25    48*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FleN7E%3A11%240

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
173.78 ms | 1398 KiB | 34 Q