3v4l.org

run code in 300+ PHP versions simultaneously
<?php if (!function_exists('json_encode')) { function json_encode($it) { $tmp = $it; if (!($assoc = is_object($it)) && !is_array($it)) { return _js_enc_value((array)$it); } $i = 0; $it = (array)$it; if (!$assoc) { foreach ($it as $k => $v) { if ($k !== $i) { $assoc = true; break; } $i++; } } $data = array(); foreach ($it as $k => $v) { $v = _js_enc_value($v); if ($assoc) { $k = _js_enc_value($k); $data[] = $k . ':' . $v; } else { $data[] = $v; } } if ($assoc) { $bl = '{'; $br = '}'; } else { $bl = '['; $br = ']'; } return $bl . implode(',', $data) . $br; } function _js_enc_value($value) { switch (true) { case is_array($value): case is_object($value): return json_encode($value); case $value === null: case is_resource($value): return 'null'; case is_bool($value): return $value ? 'true' : 'false'; case is_string($value): return '"' . _js_uc_esc($value) . '"'; default: return $value; } } function _js_uc_esc($value) { $pattern = "@[\x08-\x10\x0c\x0d\\\\\"/]|([^\x08-\x10\x0c\x0d\x32-\x7f\\\\\"/])++@"; return preg_replace_callback($pattern, '_js_uc_esc_cb', $value); } function _js_uc_esc_cb($matches) { switch (true) { case isset($matches[1]): $char = mb_convert_encoding($matches[0], 'UTF-16', 'UTF-8'); $str = ''; $len = strlen($char); for ($i = 0; $i < $len; $i += 2) { $str .= sprintf('\\u%02x%02x', ord($char[$i]), ord($char[$i + 1]) ); } return $str; default: return addcslashes($matches[0], "\x08\x09\x10\x0c\x0d\\\"/"); } } } $var = array( array( 'text', 1, 1.1, true, false, null, array(), new stdClass, fopen('php://stdin', 'r'), ), array( '0' => 'hoge', '1' => 'foo', ), (object)array( '0' => 'hoge', '1' => 'foo', ), array( '0' => 'hoge', '2' => 'foo', ), (object)array( '0' => 'hoge', '2' => 'foo', ), ); var_dump(@json_encode($var));
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 9
Branch analysis from position: 5
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
filename:       /in/O8ZLJ
function name:  (null)
number of ops:  41
compiled vars:  !0 = $var
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   INIT_FCALL                                               'function_exists'
          1        SEND_VAL                                                 'json_encode'
          2        DO_ICALL                                         $1      
          3        BOOL_NOT                                         ~2      $1
          4      > JMPZ                                                     ~2, ->9
    5     5    >   DECLARE_FUNCTION                                         'json_encode'
   41     6        DECLARE_FUNCTION                                         '_js_enc_value'
   58     7        DECLARE_FUNCTION                                         '_js_uc_esc'
   63     8        DECLARE_FUNCTION                                         '_js_uc_esc_cb'
   85     9    >   INIT_ARRAY                                       ~3      'text'
   86    10        ADD_ARRAY_ELEMENT                                ~3      1
   87    11        ADD_ARRAY_ELEMENT                                ~3      1.1
   85    12        ADD_ARRAY_ELEMENT                                ~3      <true>
         13        ADD_ARRAY_ELEMENT                                ~3      <false>
         14        ADD_ARRAY_ELEMENT                                ~3      null
         15        ADD_ARRAY_ELEMENT                                ~3      <array>
   92    16        NEW                                              $4      'stdClass'
         17        DO_FCALL                                      0          
         18        ADD_ARRAY_ELEMENT                                ~3      $4
   93    19        INIT_FCALL                                               'fopen'
         20        SEND_VAL                                                 'php%3A%2F%2Fstdin'
         21        SEND_VAL                                                 'r'
         22        DO_ICALL                                         $6      
         23        ADD_ARRAY_ELEMENT                                ~3      $6
         24        INIT_ARRAY                                       ~7      ~3
   85    25        ADD_ARRAY_ELEMENT                                ~7      <array>
  100    26        CAST                                          8  ~8      <array>
         27        ADD_ARRAY_ELEMENT                                ~7      ~8
   85    28        ADD_ARRAY_ELEMENT                                ~7      <array>
  108    29        CAST                                          8  ~9      <array>
         30        ADD_ARRAY_ELEMENT                                ~7      ~9
   83    31        ASSIGN                                                   !0, ~7
  113    32        INIT_FCALL                                               'var_dump'
         33        BEGIN_SILENCE                                    ~11     
         34        INIT_FCALL                                               'json_encode'
         35        SEND_VAR                                                 !0
         36        DO_ICALL                                         $12     
         37        END_SILENCE                                              ~11
         38        SEND_VAR                                                 $12
         39        DO_ICALL                                                 
         40      > RETURN                                                   1

Function %00json_encode%2Fin%2FO8ZLJ%3A5%240:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 46) Position 1 = 6, Position 2 = 9
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 15
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
2 jumps found. (Code = 43) Position 1 = 20, Position 2 = 30
Branch analysis from position: 20
2 jumps found. (Code = 77) Position 1 = 21, Position 2 = 29
Branch analysis from position: 21
2 jumps found. (Code = 78) Position 1 = 22, Position 2 = 29
Branch analysis from position: 22
2 jumps found. (Code = 43) Position 1 = 25, Position 2 = 27
Branch analysis from position: 25
1 jumps found. (Code = 42) Position 1 = 29
Branch analysis from position: 29
2 jumps found. (Code = 77) Position 1 = 32, Position 2 = 51
Branch analysis from position: 32
2 jumps found. (Code = 78) Position 1 = 33, Position 2 = 51
Branch analysis from position: 33
2 jumps found. (Code = 43) Position 1 = 39, Position 2 = 48
Branch analysis from position: 39
1 jumps found. (Code = 42) Position 1 = 50
Branch analysis from position: 50
1 jumps found. (Code = 42) Position 1 = 32
Branch analysis from position: 32
Branch analysis from position: 48
1 jumps found. (Code = 42) Position 1 = 32
Branch analysis from position: 32
Branch analysis from position: 51
2 jumps found. (Code = 43) Position 1 = 53, Position 2 = 56
Branch analysis from position: 53
1 jumps found. (Code = 42) Position 1 = 58
Branch analysis from position: 58
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 56
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 51
Branch analysis from position: 27
1 jumps found. (Code = 42) Position 1 = 21
Branch analysis from position: 21
Branch analysis from position: 29
Branch analysis from position: 29
Branch analysis from position: 30
Branch analysis from position: 9
filename:       /in/O8ZLJ
function name:  json_encode
number of ops:  66
compiled vars:  !0 = $it, !1 = $tmp, !2 = $assoc, !3 = $i, !4 = $v, !5 = $k, !6 = $data, !7 = $bl, !8 = $br
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    5     0  E >   RECV                                             !0      
    6     1        ASSIGN                                                   !1, !0
    7     2        TYPE_CHECK                                  256  ~10     !0
          3        ASSIGN                                           ~11     !2, ~10
          4        BOOL_NOT                                         ~12     ~11
          5      > JMPZ_EX                                          ~12     ~12, ->9
          6    >   TYPE_CHECK                                  128  ~13     !0
          7        BOOL_NOT                                         ~14     ~13
          8        BOOL                                             ~12     ~14
          9    > > JMPZ                                                     ~12, ->15
    8    10    >   INIT_FCALL_BY_NAME                                       '_js_enc_value'
         11        CAST                                          7  ~15     !0
         12        SEND_VAL_EX                                              ~15
         13        DO_FCALL                                      0  $16     
         14      > RETURN                                                   $16
   10    15    >   ASSIGN                                                   !3, 0
   11    16        CAST                                          7  ~18     !0
         17        ASSIGN                                                   !0, ~18
   12    18        BOOL_NOT                                         ~20     !2
         19      > JMPZ                                                     ~20, ->30
   13    20    > > FE_RESET_R                                       $21     !0, ->29
         21    > > FE_FETCH_R                                       ~22     $21, !4, ->29
         22    >   ASSIGN                                                   !5, ~22
   14    23        IS_NOT_IDENTICAL                                         !5, !3
         24      > JMPZ                                                     ~24, ->27
   15    25    >   ASSIGN                                                   !2, <true>
   16    26      > JMP                                                      ->29
   18    27    >   PRE_INC                                                  !3
   13    28      > JMP                                                      ->21
         29    >   FE_FREE                                                  $21
   21    30    >   ASSIGN                                                   !6, <array>
   22    31      > FE_RESET_R                                       $28     !0, ->51
         32    > > FE_FETCH_R                                       ~29     $28, !4, ->51
         33    >   ASSIGN                                                   !5, ~29
   23    34        INIT_FCALL_BY_NAME                                       '_js_enc_value'
         35        SEND_VAR_EX                                              !4
         36        DO_FCALL                                      0  $31     
         37        ASSIGN                                                   !4, $31
   24    38      > JMPZ                                                     !2, ->48
   25    39    >   INIT_FCALL_BY_NAME                                       '_js_enc_value'
         40        SEND_VAR_EX                                              !5
         41        DO_FCALL                                      0  $33     
         42        ASSIGN                                                   !5, $33
   26    43        CONCAT                                           ~36     !5, '%3A'
         44        CONCAT                                           ~37     ~36, !4
         45        ASSIGN_DIM                                               !6
         46        OP_DATA                                                  ~37
         47      > JMP                                                      ->50
   28    48    >   ASSIGN_DIM                                               !6
         49        OP_DATA                                                  !4
   22    50    > > JMP                                                      ->32
         51    >   FE_FREE                                                  $28
   31    52      > JMPZ                                                     !2, ->56
   32    53    >   ASSIGN                                                   !7, '%7B'
   33    54        ASSIGN                                                   !8, '%7D'
         55      > JMP                                                      ->58
   35    56    >   ASSIGN                                                   !7, '%5B'
   36    57        ASSIGN                                                   !8, '%5D'
   38    58    >   INIT_FCALL                                               'implode'
         59        SEND_VAL                                                 '%2C'
         60        SEND_VAR                                                 !6
         61        DO_ICALL                                         $43     
         62        CONCAT                                           ~44     !7, $43
         63        CONCAT                                           ~45     ~44, !8
         64      > RETURN                                                   ~45
   39    65*     > RETURN                                                   null

End of function %00json_encode%2Fin%2FO8ZLJ%3A5%240

Function %00_js_enc_value%2Fin%2FO8ZLJ%3A41%241:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 44) Position 1 = 3, Position 2 = 14
Branch analysis from position: 3
2 jumps found. (Code = 44) Position 1 = 5, Position 2 = 14
Branch analysis from position: 5
2 jumps found. (Code = 44) Position 1 = 7, Position 2 = 18
Branch analysis from position: 7
2 jumps found. (Code = 44) Position 1 = 9, Position 2 = 18
Branch analysis from position: 9
2 jumps found. (Code = 44) Position 1 = 11, Position 2 = 19
Branch analysis from position: 11
2 jumps found. (Code = 44) Position 1 = 13, Position 2 = 24
Branch analysis from position: 13
1 jumps found. (Code = 42) Position 1 = 30
Branch analysis from position: 30
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 24
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 19
2 jumps found. (Code = 43) Position 1 = 20, Position 2 = 22
Branch analysis from position: 20
1 jumps found. (Code = 42) Position 1 = 23
Branch analysis from position: 23
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 22
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 18
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 18
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 14
filename:       /in/O8ZLJ
function name:  _js_enc_value
number of ops:  32
compiled vars:  !0 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   41     0  E >   RECV                                             !0      
   43     1        TYPE_CHECK                                  128          !0
          2      > JMPNZ                                                    ~2, ->14
   44     3    >   TYPE_CHECK                                  256          !0
          4      > JMPNZ                                                    ~3, ->14
   46     5    >   TYPE_CHECK                                    2          !0
          6      > JMPNZ                                                    ~4, ->18
   47     7    >   TYPE_CHECK                                  512          !0
          8      > JMPNZ                                                    ~5, ->18
   49     9    >   TYPE_CHECK                                   12          !0
         10      > JMPNZ                                                    ~6, ->19
   51    11    >   TYPE_CHECK                                   64          !0
         12      > JMPNZ                                                    ~7, ->24
         13    > > JMP                                                      ->30
   45    14    >   INIT_FCALL                                               'json_encode'
         15        SEND_VAR                                                 !0
         16        DO_ICALL                                         $8      
         17      > RETURN                                                   $8
   48    18    > > RETURN                                                   'null'
   50    19    > > JMPZ                                                     !0, ->22
         20    >   QM_ASSIGN                                        ~9      'true'
         21      > JMP                                                      ->23
         22    >   QM_ASSIGN                                        ~9      'false'
         23    > > RETURN                                                   ~9
   52    24    >   INIT_FCALL_BY_NAME                                       '_js_uc_esc'
         25        SEND_VAR_EX                                              !0
         26        DO_FCALL                                      0  $10     
         27        CONCAT                                           ~11     '%22', $10
         28        CONCAT                                           ~12     ~11, '%22'
         29      > RETURN                                                   ~12
   54    30    > > RETURN                                                   !0
   56    31*     > RETURN                                                   null

End of function %00_js_enc_value%2Fin%2FO8ZLJ%3A41%241

Function %00_js_uc_esc%2Fin%2FO8ZLJ%3A58%242:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/O8ZLJ
function name:  _js_uc_esc
number of ops:  9
compiled vars:  !0 = $value, !1 = $pattern
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   58     0  E >   RECV                                             !0      
   59     1        ASSIGN                                                   !1, '%40%5B%08-%10%0C%0D%5C%5C%22%2F%5D%7C%28%5B%5E%08-%10%0C%0D2-%7F%5C%5C%22%2F%5D%29%2B%2B%40'
   60     2        INIT_FCALL                                               'preg_replace_callback'
          3        SEND_VAR                                                 !1
          4        SEND_VAL                                                 '_js_uc_esc_cb'
          5        SEND_VAR                                                 !0
          6        DO_ICALL                                         $3      
          7      > RETURN                                                   $3
   61     8*     > RETURN                                                   null

End of function %00_js_uc_esc%2Fin%2FO8ZLJ%3A58%242

Function %00_js_uc_esc_cb%2Fin%2FO8ZLJ%3A63%243:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 44) Position 1 = 3, Position 2 = 4
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 35
Branch analysis from position: 35
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 32
Branch analysis from position: 32
2 jumps found. (Code = 44) Position 1 = 34, Position 2 = 16
Branch analysis from position: 34
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 16
2 jumps found. (Code = 44) Position 1 = 34, Position 2 = 16
Branch analysis from position: 34
Branch analysis from position: 16
filename:       /in/O8ZLJ
function name:  _js_uc_esc_cb
number of ops:  42
compiled vars:  !0 = $matches, !1 = $char, !2 = $str, !3 = $len, !4 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   63     0  E >   RECV                                             !0      
   65     1        ISSET_ISEMPTY_DIM_OBJ                         0          !0, 1
          2      > JMPNZ                                                    ~6, ->4
          3    > > JMP                                                      ->35
   66     4    >   INIT_FCALL                                               'mb_convert_encoding'
          5        FETCH_DIM_R                                      ~7      !0, 0
          6        SEND_VAL                                                 ~7
          7        SEND_VAL                                                 'UTF-16'
          8        SEND_VAL                                                 'UTF-8'
          9        DO_ICALL                                         $8      
         10        ASSIGN                                                   !1, $8
   67    11        ASSIGN                                                   !2, ''
   68    12        STRLEN                                           ~11     !1
         13        ASSIGN                                                   !3, ~11
   69    14        ASSIGN                                                   !4, 0
         15      > JMP                                                      ->32
   70    16    >   INIT_FCALL                                               'sprintf'
         17        SEND_VAL                                                 '%5Cu%2502x%2502x'
   71    18        INIT_FCALL                                               'ord'
         19        FETCH_DIM_R                                      ~14     !1, !4
         20        SEND_VAL                                                 ~14
         21        DO_ICALL                                         $15     
         22        SEND_VAR                                                 $15
   72    23        INIT_FCALL                                               'ord'
         24        ADD                                              ~16     !4, 1
         25        FETCH_DIM_R                                      ~17     !1, ~16
         26        SEND_VAL                                                 ~17
         27        DO_ICALL                                         $18     
         28        SEND_VAR                                                 $18
         29        DO_ICALL                                         $19     
         30        ASSIGN_OP                                     8          !2, $19
   69    31        ASSIGN_OP                                     1          !4, 2
         32    >   IS_SMALLER                                               !4, !3
         33      > JMPNZ                                                    ~22, ->16
   75    34    > > RETURN                                                   !2
   77    35    >   INIT_FCALL                                               'addcslashes'
         36        FETCH_DIM_R                                      ~23     !0, 0
         37        SEND_VAL                                                 ~23
         38        SEND_VAL                                                 '%08%09%10%0C%0D%5C%22%2F'
         39        DO_ICALL                                         $24     
         40      > RETURN                                                   $24
   79    41*     > RETURN                                                   null

End of function %00_js_uc_esc_cb%2Fin%2FO8ZLJ%3A63%243

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
159.57 ms | 1416 KiB | 33 Q