3v4l.org

run code in 300+ PHP versions simultaneously
<?php if (!function_exists('json_encode')) { function json_encode($it) { if (!($assoc = is_object($it)) && !is_array($it)) { return _js_enc_value($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 = (string)$k; if (preg_match('//u', $k)) { $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) && preg_match('//u', $value): return '"' . _js_uc_esc($value) . '"'; default: return $value; } } function _js_uc_esc($value) { $pattern = "@[\x08-\x0a\x0c\x0d\\\\\"/]|([^\x08-\x0a\x0c\x0d\x20-\x7f\\\\\"/])++@u"; 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\x0a\x0c\x0d\\\"/"); } } } $var = array( array( 'text', 1, 1.1, true, false, null, array(), new stdClass, ), array( '&\'/"' => "\r\n\t", '' => '', ), 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/DH5tv
function name:  (null)
number of ops:  35
compiled vars:  !0 = $var
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    4     0  E >   INIT_FCALL                                               'function_exists'
          1        SEND_VAL                                                 'json_encode'
          2        DO_ICALL                                         $1      
          3        BOOL_NOT                                         ~2      $1
          4      > JMPZ                                                     ~2, ->9
    6     5    >   DECLARE_FUNCTION                                         'json_encode'
   44     6        DECLARE_FUNCTION                                         '_js_enc_value'
   61     7        DECLARE_FUNCTION                                         '_js_uc_esc'
   66     8        DECLARE_FUNCTION                                         '_js_uc_esc_cb'
   88     9    >   INIT_ARRAY                                       ~3      'text'
   89    10        ADD_ARRAY_ELEMENT                                ~3      1
   90    11        ADD_ARRAY_ELEMENT                                ~3      1.1
   88    12        ADD_ARRAY_ELEMENT                                ~3      <true>
         13        ADD_ARRAY_ELEMENT                                ~3      <false>
         14        ADD_ARRAY_ELEMENT                                ~3      null
         15        ADD_ARRAY_ELEMENT                                ~3      <array>
   95    16        NEW                                              $4      'stdClass'
         17        DO_FCALL                                      0          
         18        ADD_ARRAY_ELEMENT                                ~3      $4
         19        INIT_ARRAY                                       ~6      ~3
   88    20        ADD_ARRAY_ELEMENT                                ~6      <array>
         21        ADD_ARRAY_ELEMENT                                ~6      <array>
  106    22        CAST                                          8  ~7      <array>
         23        ADD_ARRAY_ELEMENT                                ~6      ~7
   88    24        ADD_ARRAY_ELEMENT                                ~6      <array>
  114    25        CAST                                          8  ~8      <array>
         26        ADD_ARRAY_ELEMENT                                ~6      ~8
   86    27        ASSIGN                                                   !0, ~6
  119    28        INIT_FCALL                                               'var_dump'
         29        INIT_FCALL                                               'json_encode'
         30        SEND_VAR                                                 !0
         31        DO_ICALL                                         $10     
         32        SEND_VAR                                                 $10
         33        DO_ICALL                                                 
         34      > RETURN                                                   1

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

End of function %00json_encode%2Fin%2FDH5tv%3A6%240

Function %00_js_enc_value%2Fin%2FDH5tv%3A44%241:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 44) Position 1 = 3, Position 2 = 20
Branch analysis from position: 3
2 jumps found. (Code = 44) Position 1 = 5, Position 2 = 20
Branch analysis from position: 5
2 jumps found. (Code = 44) Position 1 = 7, Position 2 = 24
Branch analysis from position: 7
2 jumps found. (Code = 44) Position 1 = 9, Position 2 = 24
Branch analysis from position: 9
2 jumps found. (Code = 44) Position 1 = 11, Position 2 = 25
Branch analysis from position: 11
2 jumps found. (Code = 46) Position 1 = 13, Position 2 = 18
Branch analysis from position: 13
2 jumps found. (Code = 44) Position 1 = 19, Position 2 = 30
Branch analysis from position: 19
1 jumps found. (Code = 42) Position 1 = 36
Branch analysis from position: 36
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 30
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 18
Branch analysis from position: 25
2 jumps found. (Code = 43) Position 1 = 26, Position 2 = 28
Branch analysis from position: 26
1 jumps found. (Code = 42) Position 1 = 29
Branch analysis from position: 29
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 28
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: 24
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 20
filename:       /in/DH5tv
function name:  _js_enc_value
number of ops:  38
compiled vars:  !0 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   44     0  E >   RECV                                             !0      
   46     1        TYPE_CHECK                                  128          !0
          2      > JMPNZ                                                    ~2, ->20
   47     3    >   TYPE_CHECK                                  256          !0
          4      > JMPNZ                                                    ~3, ->20
   49     5    >   TYPE_CHECK                                    2          !0
          6      > JMPNZ                                                    ~4, ->24
   50     7    >   TYPE_CHECK                                  512          !0
          8      > JMPNZ                                                    ~5, ->24
   52     9    >   TYPE_CHECK                                   12          !0
         10      > JMPNZ                                                    ~6, ->25
   54    11    >   TYPE_CHECK                                   64  ~7      !0
         12      > JMPZ_EX                                          ~7      ~7, ->18
         13    >   INIT_FCALL                                               'preg_match'
         14        SEND_VAL                                                 '%2F%2Fu'
         15        SEND_VAR                                                 !0
         16        DO_ICALL                                         $8      
         17        BOOL                                             ~7      $8
         18    > > JMPNZ                                                    ~7, ->30
         19    > > JMP                                                      ->36
   48    20    >   INIT_FCALL                                               'json_encode'
         21        SEND_VAR                                                 !0
         22        DO_ICALL                                         $9      
         23      > RETURN                                                   $9
   51    24    > > RETURN                                                   'null'
   53    25    > > JMPZ                                                     !0, ->28
         26    >   QM_ASSIGN                                        ~10     'true'
         27      > JMP                                                      ->29
         28    >   QM_ASSIGN                                        ~10     'false'
         29    > > RETURN                                                   ~10
   55    30    >   INIT_FCALL_BY_NAME                                       '_js_uc_esc'
         31        SEND_VAR_EX                                              !0
         32        DO_FCALL                                      0  $11     
         33        CONCAT                                           ~12     '%22', $11
         34        CONCAT                                           ~13     ~12, '%22'
         35      > RETURN                                                   ~13
   57    36    > > RETURN                                                   !0
   59    37*     > RETURN                                                   null

End of function %00_js_enc_value%2Fin%2FDH5tv%3A44%241

Function %00_js_uc_esc%2Fin%2FDH5tv%3A61%242:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/DH5tv
function name:  _js_uc_esc
number of ops:  9
compiled vars:  !0 = $value, !1 = $pattern
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   61     0  E >   RECV                                             !0      
   62     1        ASSIGN                                                   !1, '%40%5B%08-%0A%0C%0D%5C%5C%22%2F%5D%7C%28%5B%5E%08-%0A%0C%0D+-%7F%5C%5C%22%2F%5D%29%2B%2B%40u'
   63     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
   64     8*     > RETURN                                                   null

End of function %00_js_uc_esc%2Fin%2FDH5tv%3A61%242

Function %00_js_uc_esc_cb%2Fin%2FDH5tv%3A66%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/DH5tv
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
-------------------------------------------------------------------------------------
   66     0  E >   RECV                                             !0      
   68     1        ISSET_ISEMPTY_DIM_OBJ                         0          !0, 1
          2      > JMPNZ                                                    ~6, ->4
          3    > > JMP                                                      ->35
   69     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
   70    11        ASSIGN                                                   !2, ''
   71    12        STRLEN                                           ~11     !1
         13        ASSIGN                                                   !3, ~11
   72    14        ASSIGN                                                   !4, 0
         15      > JMP                                                      ->32
   73    16    >   INIT_FCALL                                               'sprintf'
         17        SEND_VAL                                                 '%5Cu%2502x%2502x'
   74    18        INIT_FCALL                                               'ord'
         19        FETCH_DIM_R                                      ~14     !1, !4
         20        SEND_VAL                                                 ~14
         21        DO_ICALL                                         $15     
         22        SEND_VAR                                                 $15
   75    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
   72    31        ASSIGN_OP                                     1          !4, 2
         32    >   IS_SMALLER                                               !4, !3
         33      > JMPNZ                                                    ~22, ->16
   78    34    > > RETURN                                                   !2
   80    35    >   INIT_FCALL                                               'addcslashes'
         36        FETCH_DIM_R                                      ~23     !0, 0
         37        SEND_VAL                                                 ~23
         38        SEND_VAL                                                 '%08%09%0A%0C%0D%5C%22%2F'
         39        DO_ICALL                                         $24     
         40      > RETURN                                                   $24
   82    41*     > RETURN                                                   null

End of function %00_js_uc_esc_cb%2Fin%2FDH5tv%3A66%243

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
164.85 ms | 1416 KiB | 33 Q