3v4l.org

run code in 300+ PHP versions simultaneously
<?php function json_is($string) { try { // try to decode string json_decode($string); } catch (ErrorException $e) { // exception has been caught which means argument wasn't a string and thus is definitely no json. return FALSE; } // check if error occured return (json_last_error() == JSON_ERROR_NONE); } /** * Checks whether a string is valid json. * * @param string $string * @return boolean */ function isJson($string, $assoc = false) { // Make sure the string is not empty and is, in fact, a string. // An empty string is not valid JSON. if(!empty($string) && is_string($string)) { json_decode($string, $assoc); return (json_last_error() == JSON_ERROR_NONE); // return json_decode($str) != null; } return false; } $a=['arraiii'=>['dentro'=>234]]; $b=json_encode($a); $c=''; var_dump(json_decode('')); var_dump(isJson($c));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/TX64O
function name:  (null)
number of ops:  19
compiled vars:  !0 = $a, !1 = $b, !2 = $c
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   66     0  E >   ASSIGN                                                   !0, <array>
   67     1        INIT_FCALL                                               'json_encode'
          2        SEND_VAR                                                 !0
          3        DO_ICALL                                         $4      
          4        ASSIGN                                                   !1, $4
   70     5        ASSIGN                                                   !2, ''
   74     6        INIT_FCALL                                               'var_dump'
          7        INIT_FCALL                                               'json_decode'
          8        SEND_VAL                                                 ''
          9        DO_ICALL                                         $7      
         10        SEND_VAR                                                 $7
         11        DO_ICALL                                                 
   76    12        INIT_FCALL                                               'var_dump'
         13        INIT_FCALL                                               'isjson'
         14        SEND_VAR                                                 !2
         15        DO_FCALL                                      0  $9      
         16        SEND_VAR                                                 $9
         17        DO_ICALL                                                 
         18      > RETURN                                                   1

Function json_is:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 7
Branch analysis from position: 7
1 jumps found. (Code = 62) Position 1 = -2
Found catch point at position: 5
Branch analysis from position: 5
2 jumps found. (Code = 107) Position 1 = 6, Position 2 = -2
Branch analysis from position: 6
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/TX64O
function name:  json_is
number of ops:  12
compiled vars:  !0 = $string, !1 = $e
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    4     0  E >   RECV                                             !0      
    9     1        INIT_FCALL                                               'json_decode'
          2        SEND_VAR                                                 !0
          3        DO_ICALL                                                 
          4      > JMP                                                      ->7
   11     5  E > > CATCH                                       last         'ErrorException'
   14     6    > > RETURN                                                   <false>
   18     7    >   INIT_FCALL                                               'json_last_error'
          8        DO_ICALL                                         $3      
          9        IS_EQUAL                                         ~4      $3, 0
         10      > RETURN                                                   ~4
   19    11*     > RETURN                                                   null

End of function json_is

Function isjson:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 46) Position 1 = 5, Position 2 = 7
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 16
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 16
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 7
filename:       /in/TX64O
function name:  isJson
number of ops:  18
compiled vars:  !0 = $string, !1 = $assoc
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   45     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      <false>
   51     2        ISSET_ISEMPTY_CV                                 ~2      !0
          3        BOOL_NOT                                         ~3      ~2
          4      > JMPZ_EX                                          ~3      ~3, ->7
          5    >   TYPE_CHECK                                   64  ~4      !0
          6        BOOL                                             ~3      ~4
          7    > > JMPZ                                                     ~3, ->16
   52     8    >   INIT_FCALL                                               'json_decode'
          9        SEND_VAR                                                 !0
         10        SEND_VAR                                                 !1
         11        DO_ICALL                                                 
   53    12        INIT_FCALL                                               'json_last_error'
         13        DO_ICALL                                         $6      
         14        IS_EQUAL                                         ~7      $6, 0
         15      > RETURN                                                   ~7
   57    16    > > RETURN                                                   <false>
   58    17*     > RETURN                                                   null

End of function isjson

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
194.08 ms | 1407 KiB | 22 Q