3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * WordPress Core functions. */ function wp_json_encode( $data, $options = 0, $depth = 512 ) { // Simulate JSON encoding failure to trigger the rest of the code. $json = false; //json_encode( $data, $options, $depth ); // If json_encode() was successful, no need to do more sanity checking. if ( false !== $json ) { return $json; } try { $data = _wp_json_sanity_check( $data, $depth ); } catch ( Exception $e ) { return false; } return json_encode( $data, $options, $depth ); } function _wp_json_sanity_check( $data, $depth ) { if ( $depth < 0 ) { throw new Exception( 'Reached depth limit' ); } if ( is_array( $data ) ) { $output = array(); foreach ( $data as $id => $el ) { // Don't forget to sanitize the ID! if ( is_string( $id ) ) { $clean_id = _wp_json_convert_string( $id ); } else { $clean_id = $id; } // Check the element type, so that we're only recursing if we really have to. if ( is_array( $el ) || is_object( $el ) ) { $output[ $clean_id ] = _wp_json_sanity_check( $el, $depth - 1 ); } elseif ( is_string( $el ) ) { $output[ $clean_id ] = _wp_json_convert_string( $el ); } else { $output[ $clean_id ] = $el; } } } elseif ( is_object( $data ) ) { $output = new stdClass; foreach ( $data as $id => $el ) { if ( is_string( $id ) ) { $clean_id = _wp_json_convert_string( $id ); } else { $clean_id = $id; } if ( is_array( $el ) || is_object( $el ) ) { $output->$clean_id = _wp_json_sanity_check( $el, $depth - 1 ); } elseif ( is_string( $el ) ) { $output->$clean_id = _wp_json_convert_string( $el ); } else { $output->$clean_id = $el; } } } elseif ( is_string( $data ) ) { return _wp_json_convert_string( $data ); } else { return $data; } var_dump( $output ); return $output; } function _wp_json_convert_string( $string ) { // Simulate failed by returning false. if ( isset( $GLOBALS['simulate_fail'] ) ) { return false; } $encoding = mb_detect_encoding( $string, mb_detect_order(), true ); if ( $encoding ) { return mb_convert_encoding( $string, 'UTF-8', $encoding ); } else { return mb_convert_encoding( $string, 'UTF-8', 'UTF-8' ); } } /** * Run some tests. */ echo "\n***** Test with an array. ****\n"; $data = array( 'blogname' => array( 'value' => 'Test', ), ); echo "\n***** When succeeds ****\n"; $json = wp_json_encode( $data ); echo "JSON = {$json}\n"; echo "\n***** When fails ****\n"; $GLOBALS['simulate_fail'] = true; $json = wp_json_encode( $data ); echo "JSON = {$json}\n";
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/n4qT6
function name:  (null)
number of ops:  24
compiled vars:  !0 = $data, !1 = $json
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   99     0  E >   ECHO                                                     '%0A%2A%2A%2A%2A%2A+Test+with+an+array.+%2A%2A%2A%2A%0A'
  101     1        ASSIGN                                                   !0, <array>
  107     2        ECHO                                                     '%0A%2A%2A%2A%2A%2A+When+succeeds+%2A%2A%2A%2A%0A'
  108     3        INIT_FCALL                                               'wp_json_encode'
          4        SEND_VAR                                                 !0
          5        DO_FCALL                                      0  $3      
          6        ASSIGN                                                   !1, $3
  109     7        ROPE_INIT                                     3  ~6      'JSON+%3D+'
          8        ROPE_ADD                                      1  ~6      ~6, !1
          9        ROPE_END                                      2  ~5      ~6, '%0A'
         10        ECHO                                                     ~5
  112    11        ECHO                                                     '%0A%2A%2A%2A%2A%2A+When+fails+%2A%2A%2A%2A%0A'
  113    12        FETCH_W                      global              $8      'GLOBALS'
         13        ASSIGN_DIM                                               $8, 'simulate_fail'
         14        OP_DATA                                                  <true>
  114    15        INIT_FCALL                                               'wp_json_encode'
         16        SEND_VAR                                                 !0
         17        DO_FCALL                                      0  $10     
         18        ASSIGN                                                   !1, $10
  115    19        ROPE_INIT                                     3  ~13     'JSON+%3D+'
         20        ROPE_ADD                                      1  ~13     ~13, !1
         21        ROPE_END                                      2  ~12     ~13, '%0A'
         22        ECHO                                                     ~12
         23      > RETURN                                                   1

Function wp_json_encode:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 7
Branch analysis from position: 6
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 7
1 jumps found. (Code = 42) Position 1 = 15
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
Found catch point at position: 13
Branch analysis from position: 13
2 jumps found. (Code = 107) Position 1 = 14, Position 2 = -2
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/n4qT6
function name:  wp_json_encode
number of ops:  22
compiled vars:  !0 = $data, !1 = $options, !2 = $depth, !3 = $json, !4 = $e
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    7     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      0
          2        RECV_INIT                                        !2      512
    9     3        ASSIGN                                                   !3, <false>
   12     4        TYPE_CHECK                                  1018          !3
          5      > JMPZ                                                     ~6, ->7
   13     6    > > RETURN                                                   !3
   17     7    >   INIT_FCALL_BY_NAME                                       '_wp_json_sanity_check'
          8        SEND_VAR_EX                                              !0
          9        SEND_VAR_EX                                              !2
         10        DO_FCALL                                      0  $7      
         11        ASSIGN                                                   !0, $7
         12      > JMP                                                      ->15
   18    13  E > > CATCH                                       last         'Exception'
   19    14    > > RETURN                                                   <false>
   22    15    >   INIT_FCALL                                               'json_encode'
         16        SEND_VAR                                                 !0
         17        SEND_VAR                                                 !1
         18        SEND_VAR                                                 !2
         19        DO_ICALL                                         $9      
         20      > RETURN                                                   $9
   23    21*     > RETURN                                                   null

End of function wp_json_encode

Function _wp_json_sanity_check:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 8
Branch analysis from position: 4
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 48
Branch analysis from position: 10
2 jumps found. (Code = 77) Position 1 = 12, Position 2 = 46
Branch analysis from position: 12
2 jumps found. (Code = 78) Position 1 = 13, Position 2 = 46
Branch analysis from position: 13
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 21
Branch analysis from position: 16
1 jumps found. (Code = 42) Position 1 = 22
Branch analysis from position: 22
2 jumps found. (Code = 47) Position 1 = 24, Position 2 = 26
Branch analysis from position: 24
2 jumps found. (Code = 43) Position 1 = 27, Position 2 = 35
Branch analysis from position: 27
1 jumps found. (Code = 42) Position 1 = 45
Branch analysis from position: 45
1 jumps found. (Code = 42) Position 1 = 12
Branch analysis from position: 12
Branch analysis from position: 35
2 jumps found. (Code = 43) Position 1 = 37, Position 2 = 43
Branch analysis from position: 37
1 jumps found. (Code = 42) Position 1 = 45
Branch analysis from position: 45
Branch analysis from position: 43
1 jumps found. (Code = 42) Position 1 = 12
Branch analysis from position: 12
Branch analysis from position: 26
Branch analysis from position: 21
2 jumps found. (Code = 47) Position 1 = 24, Position 2 = 26
Branch analysis from position: 24
Branch analysis from position: 26
Branch analysis from position: 46
1 jumps found. (Code = 42) Position 1 = 98
Branch analysis from position: 98
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 46
Branch analysis from position: 48
2 jumps found. (Code = 43) Position 1 = 50, Position 2 = 90
Branch analysis from position: 50
2 jumps found. (Code = 77) Position 1 = 54, Position 2 = 88
Branch analysis from position: 54
2 jumps found. (Code = 78) Position 1 = 55, Position 2 = 88
Branch analysis from position: 55
2 jumps found. (Code = 43) Position 1 = 58, Position 2 = 63
Branch analysis from position: 58
1 jumps found. (Code = 42) Position 1 = 64
Branch analysis from position: 64
2 jumps found. (Code = 47) Position 1 = 66, Position 2 = 68
Branch analysis from position: 66
2 jumps found. (Code = 43) Position 1 = 69, Position 2 = 77
Branch analysis from position: 69
1 jumps found. (Code = 42) Position 1 = 87
Branch analysis from position: 87
1 jumps found. (Code = 42) Position 1 = 54
Branch analysis from position: 54
Branch analysis from position: 77
2 jumps found. (Code = 43) Position 1 = 79, Position 2 = 85
Branch analysis from position: 79
1 jumps found. (Code = 42) Position 1 = 87
Branch analysis from position: 87
Branch analysis from position: 85
1 jumps found. (Code = 42) Position 1 = 54
Branch analysis from position: 54
Branch analysis from position: 68
Branch analysis from position: 63
2 jumps found. (Code = 47) Position 1 = 66, Position 2 = 68
Branch analysis from position: 66
Branch analysis from position: 68
Branch analysis from position: 88
1 jumps found. (Code = 42) Position 1 = 98
Branch analysis from position: 98
Branch analysis from position: 88
Branch analysis from position: 90
2 jumps found. (Code = 43) Position 1 = 92, Position 2 = 97
Branch analysis from position: 92
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 97
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/n4qT6
function name:  _wp_json_sanity_check
number of ops:  103
compiled vars:  !0 = $data, !1 = $depth, !2 = $output, !3 = $el, !4 = $id, !5 = $clean_id
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   25     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   26     2        IS_SMALLER                                               !1, 0
          3      > JMPZ                                                     ~6, ->8
   27     4    >   NEW                                              $7      'Exception'
          5        SEND_VAL_EX                                              'Reached+depth+limit'
          6        DO_FCALL                                      0          
          7      > THROW                                         0          $7
   30     8    >   TYPE_CHECK                                  128          !0
          9      > JMPZ                                                     ~9, ->48
   31    10    >   ASSIGN                                                   !2, <array>
   32    11      > FE_RESET_R                                       $11     !0, ->46
         12    > > FE_FETCH_R                                       ~12     $11, !3, ->46
         13    >   ASSIGN                                                   !4, ~12
   34    14        TYPE_CHECK                                   64          !4
         15      > JMPZ                                                     ~14, ->21
   35    16    >   INIT_FCALL_BY_NAME                                       '_wp_json_convert_string'
         17        SEND_VAR_EX                                              !4
         18        DO_FCALL                                      0  $15     
         19        ASSIGN                                                   !5, $15
         20      > JMP                                                      ->22
   37    21    >   ASSIGN                                                   !5, !4
   41    22    >   TYPE_CHECK                                  128  ~18     !3
         23      > JMPNZ_EX                                         ~18     ~18, ->26
         24    >   TYPE_CHECK                                  256  ~19     !3
         25        BOOL                                             ~18     ~19
         26    > > JMPZ                                                     ~18, ->35
   42    27    >   INIT_FCALL_BY_NAME                                       '_wp_json_sanity_check'
         28        SEND_VAR_EX                                              !3
         29        SUB                                              ~21     !1, 1
         30        SEND_VAL_EX                                              ~21
         31        DO_FCALL                                      0  $22     
         32        ASSIGN_DIM                                               !2, !5
         33        OP_DATA                                                  $22
         34      > JMP                                                      ->45
   43    35    >   TYPE_CHECK                                   64          !3
         36      > JMPZ                                                     ~23, ->43
   44    37    >   INIT_FCALL_BY_NAME                                       '_wp_json_convert_string'
         38        SEND_VAR_EX                                              !3
         39        DO_FCALL                                      0  $25     
         40        ASSIGN_DIM                                               !2, !5
         41        OP_DATA                                                  $25
         42      > JMP                                                      ->45
   46    43    >   ASSIGN_DIM                                               !2, !5
         44        OP_DATA                                                  !3
   32    45    > > JMP                                                      ->12
         46    >   FE_FREE                                                  $11
         47      > JMP                                                      ->98
   49    48    >   TYPE_CHECK                                  256          !0
         49      > JMPZ                                                     ~27, ->90
   50    50    >   NEW                                              $28     'stdClass'
         51        DO_FCALL                                      0          
         52        ASSIGN                                                   !2, $28
   51    53      > FE_RESET_R                                       $31     !0, ->88
         54    > > FE_FETCH_R                                       ~32     $31, !3, ->88
         55    >   ASSIGN                                                   !4, ~32
   52    56        TYPE_CHECK                                   64          !4
         57      > JMPZ                                                     ~34, ->63
   53    58    >   INIT_FCALL_BY_NAME                                       '_wp_json_convert_string'
         59        SEND_VAR_EX                                              !4
         60        DO_FCALL                                      0  $35     
         61        ASSIGN                                                   !5, $35
         62      > JMP                                                      ->64
   55    63    >   ASSIGN                                                   !5, !4
   58    64    >   TYPE_CHECK                                  128  ~38     !3
         65      > JMPNZ_EX                                         ~38     ~38, ->68
         66    >   TYPE_CHECK                                  256  ~39     !3
         67        BOOL                                             ~38     ~39
         68    > > JMPZ                                                     ~38, ->77
   59    69    >   INIT_FCALL_BY_NAME                                       '_wp_json_sanity_check'
         70        SEND_VAR_EX                                              !3
         71        SUB                                              ~41     !1, 1
         72        SEND_VAL_EX                                              ~41
         73        DO_FCALL                                      0  $42     
         74        ASSIGN_OBJ                                               !2, !5
         75        OP_DATA                                                  $42
         76      > JMP                                                      ->87
   60    77    >   TYPE_CHECK                                   64          !3
         78      > JMPZ                                                     ~43, ->85
   61    79    >   INIT_FCALL_BY_NAME                                       '_wp_json_convert_string'
         80        SEND_VAR_EX                                              !3
         81        DO_FCALL                                      0  $45     
         82        ASSIGN_OBJ                                               !2, !5
         83        OP_DATA                                                  $45
         84      > JMP                                                      ->87
   63    85    >   ASSIGN_OBJ                                               !2, !5
         86        OP_DATA                                                  !3
   51    87    > > JMP                                                      ->54
         88    >   FE_FREE                                                  $31
         89      > JMP                                                      ->98
   66    90    >   TYPE_CHECK                                   64          !0
         91      > JMPZ                                                     ~47, ->97
   67    92    >   INIT_FCALL_BY_NAME                                       '_wp_json_convert_string'
         93        SEND_VAR_EX                                              !0
         94        DO_FCALL                                      0  $48     
         95      > RETURN                                                   $48
         96*       JMP                                                      ->98
   69    97    > > RETURN                                                   !0
   73    98    >   INIT_FCALL                                               'var_dump'
         99        SEND_VAR                                                 !2
        100        DO_ICALL                                                 
   75   101      > RETURN                                                   !2
   76   102*     > RETURN                                                   null

End of function _wp_json_sanity_check

Function _wp_json_convert_string:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 5
Branch analysis from position: 4
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 14, Position 2 = 21
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 21
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/n4qT6
function name:  _wp_json_convert_string
number of ops:  28
compiled vars:  !0 = $string, !1 = $encoding
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   78     0  E >   RECV                                             !0      
   80     1        FETCH_IS                                         ~2      'GLOBALS'
          2        ISSET_ISEMPTY_DIM_OBJ                         0          ~2, 'simulate_fail'
          3      > JMPZ                                                     ~3, ->5
   81     4    > > RETURN                                                   <false>
   84     5    >   INIT_FCALL                                               'mb_detect_encoding'
          6        SEND_VAR                                                 !0
          7        INIT_FCALL                                               'mb_detect_order'
          8        DO_ICALL                                         $4      
          9        SEND_VAR                                                 $4
         10        SEND_VAL                                                 <true>
         11        DO_ICALL                                         $5      
         12        ASSIGN                                                   !1, $5
   85    13      > JMPZ                                                     !1, ->21
   86    14    >   INIT_FCALL                                               'mb_convert_encoding'
         15        SEND_VAR                                                 !0
         16        SEND_VAL                                                 'UTF-8'
         17        SEND_VAR                                                 !1
         18        DO_ICALL                                         $7      
         19      > RETURN                                                   $7
         20*       JMP                                                      ->27
   88    21    >   INIT_FCALL                                               'mb_convert_encoding'
         22        SEND_VAR                                                 !0
         23        SEND_VAL                                                 'UTF-8'
         24        SEND_VAL                                                 'UTF-8'
         25        DO_ICALL                                         $8      
         26      > RETURN                                                   $8
   91    27*     > RETURN                                                   null

End of function _wp_json_convert_string

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
189.99 ms | 964 KiB | 26 Q