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 = $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 = '"'. addcslashes($k, '"\\') . '"'; $data[] = $k . ':' . $v; } else { $data[] = $v; } } if ($assoc) { $spl = '{'; $spr = '}'; } else { $spl = '['; $spr = ']'; } return $spl . implode(',', $data) . $spr; } function _js_enc_value($value) { switch (true) { case is_array($value): case is_object($value): return json_encode($value); case is_bool($value): return $value ? 'true' : 'false'; case $value === null: case is_resource($value) && _js_error(): return 'null'; case is_string($value): return '"' . _js_uc_esc($value) . '"'; default: return $value; } } function _js_error() { return trigger_error('json_encode(): type is unsupported, encoded as null', E_USER_WARNING); } function _js_uc_esc($value) { $pattern = "/\r|(\n)|((?:[^\x09\x0A\x0D\x20-\x7E]{3})+)/"; return preg_replace_callback($pattern, '_js_uc_esc_cb', $value); } function _js_uc_esc_cb($matches) { switch (true) { case isset($matches[2]): $char = mb_convert_encoding($matches[2], '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; case isset($matches[1]): return '\\n'; default: return '\\r'; } } } $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 = 10
Branch analysis from position: 5
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
filename:       /in/CKO38
function name:  (null)
number of ops:  42
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, ->10
    5     5    >   DECLARE_FUNCTION                                         'json_encode'
   40     6        DECLARE_FUNCTION                                         '_js_enc_value'
   57     7        DECLARE_FUNCTION                                         '_js_error'
   61     8        DECLARE_FUNCTION                                         '_js_uc_esc'
   66     9        DECLARE_FUNCTION                                         '_js_uc_esc_cb'
   87    10    >   INIT_ARRAY                                       ~3      'text'
   88    11        ADD_ARRAY_ELEMENT                                ~3      1
   89    12        ADD_ARRAY_ELEMENT                                ~3      1.1
   87    13        ADD_ARRAY_ELEMENT                                ~3      <true>
         14        ADD_ARRAY_ELEMENT                                ~3      <false>
         15        ADD_ARRAY_ELEMENT                                ~3      null
         16        ADD_ARRAY_ELEMENT                                ~3      <array>
   94    17        NEW                                              $4      'stdClass'
         18        DO_FCALL                                      0          
         19        ADD_ARRAY_ELEMENT                                ~3      $4
   95    20        BEGIN_SILENCE                                    ~6      
         21        INIT_FCALL                                               'fopen'
         22        SEND_VAL                                                 'php%3A%2F%2Fstdin'
         23        SEND_VAL                                                 'r'
         24        DO_ICALL                                         $7      
         25        END_SILENCE                                              ~6
         26        ADD_ARRAY_ELEMENT                                ~3      $7
         27        INIT_ARRAY                                       ~8      ~3
   87    28        ADD_ARRAY_ELEMENT                                ~8      <array>
  102    29        CAST                                          8  ~9      <array>
         30        ADD_ARRAY_ELEMENT                                ~8      ~9
   87    31        ADD_ARRAY_ELEMENT                                ~8      <array>
  110    32        CAST                                          8  ~10     <array>
         33        ADD_ARRAY_ELEMENT                                ~8      ~10
   85    34        ASSIGN                                                   !0, ~8
  115    35        INIT_FCALL                                               'var_dump'
         36        INIT_FCALL                                               'json_encode'
         37        SEND_VAR                                                 !0
         38        DO_ICALL                                         $12     
         39        SEND_VAR                                                 $12
         40        DO_ICALL                                                 
         41      > RETURN                                                   1

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

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

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

End of function %00_js_enc_value%2Fin%2FCKO38%3A40%241

Function %00_js_error%2Fin%2FCKO38%3A57%242:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/CKO38
function name:  _js_error
number of ops:  6
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   58     0  E >   INIT_FCALL                                               'trigger_error'
          1        SEND_VAL                                                 'json_encode%28%29%3A+type+is+unsupported%2C+encoded+as+null'
          2        SEND_VAL                                                 512
          3        DO_ICALL                                         $0      
          4      > RETURN                                                   $0
   59     5*     > RETURN                                                   null

End of function %00_js_error%2Fin%2FCKO38%3A57%242

Function %00_js_uc_esc%2Fin%2FCKO38%3A61%243:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/CKO38
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, '%2F%0D%7C%28%0A%29%7C%28%28%3F%3A%5B%5E%09%0A%0D+-%7E%5D%7B3%7D%29%2B%29%2F'
   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%2FCKO38%3A61%243

Function %00_js_uc_esc_cb%2Fin%2FCKO38%3A66%244:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 44) Position 1 = 3, Position 2 = 6
Branch analysis from position: 3
2 jumps found. (Code = 44) Position 1 = 5, Position 2 = 37
Branch analysis from position: 5
1 jumps found. (Code = 42) Position 1 = 38
Branch analysis from position: 38
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 37
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 6
1 jumps found. (Code = 42) Position 1 = 34
Branch analysis from position: 34
2 jumps found. (Code = 44) Position 1 = 36, Position 2 = 18
Branch analysis from position: 36
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 18
2 jumps found. (Code = 44) Position 1 = 36, Position 2 = 18
Branch analysis from position: 36
Branch analysis from position: 18
filename:       /in/CKO38
function name:  _js_uc_esc_cb
number of ops:  40
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, 2
          2      > JMPNZ                                                    ~6, ->6
   76     3    >   ISSET_ISEMPTY_DIM_OBJ                         0          !0, 1
          4      > JMPNZ                                                    ~7, ->37
          5    > > JMP                                                      ->38
   69     6    >   INIT_FCALL                                               'mb_convert_encoding'
          7        FETCH_DIM_R                                      ~8      !0, 2
          8        SEND_VAL                                                 ~8
          9        SEND_VAL                                                 'UTF-16'
         10        SEND_VAL                                                 'UTF-8'
         11        DO_ICALL                                         $9      
         12        ASSIGN                                                   !1, $9
   70    13        ASSIGN                                                   !2, ''
   71    14        STRLEN                                           ~12     !1
         15        ASSIGN                                                   !3, ~12
   72    16        ASSIGN                                                   !4, 0
         17      > JMP                                                      ->34
   73    18    >   INIT_FCALL                                               'sprintf'
         19        SEND_VAL                                                 '%5Cu%2502x%2502x'
         20        INIT_FCALL                                               'ord'
         21        FETCH_DIM_R                                      ~15     !1, !4
         22        SEND_VAL                                                 ~15
         23        DO_ICALL                                         $16     
         24        SEND_VAR                                                 $16
         25        INIT_FCALL                                               'ord'
         26        ADD                                              ~17     !4, 1
         27        FETCH_DIM_R                                      ~18     !1, ~17
         28        SEND_VAL                                                 ~18
         29        DO_ICALL                                         $19     
         30        SEND_VAR                                                 $19
         31        DO_ICALL                                         $20     
         32        ASSIGN_OP                                     8          !2, $20
   72    33        ASSIGN_OP                                     1          !4, 2
         34    >   IS_SMALLER                                               !4, !3
         35      > JMPNZ                                                    ~23, ->18
   75    36    > > RETURN                                                   !2
   77    37    > > RETURN                                                   '%5Cn'
   79    38    > > RETURN                                                   '%5Cr'
   81    39*     > RETURN                                                   null

End of function %00_js_uc_esc_cb%2Fin%2FCKO38%3A66%244

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
175.85 ms | 1416 KiB | 35 Q