3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Checks whether a string is valid json. * * @param string $string * @return boolean */ 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); } // Make sure the string is not empty and is, in fact, a string. An empty string is not valid JSON. function isJson($string, $assoc = false) { json_decode($string, $assoc); 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(isJson($c));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/2CmLp
function name:  (null)
number of ops:  13
compiled vars:  !0 = $a, !1 = $b, !2 = $c
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   67     0  E >   ASSIGN                                                   !0, <array>
   68     1        INIT_FCALL                                               'json_encode'
          2        SEND_VAR                                                 !0
          3        DO_ICALL                                         $4      
          4        ASSIGN                                                   !1, $4
   71     5        ASSIGN                                                   !2, ''
   73     6        INIT_FCALL                                               'var_dump'
          7        INIT_FCALL                                               'isjson'
          8        SEND_VAR                                                 !2
          9        DO_FCALL                                      0  $7      
         10        SEND_VAR                                                 $7
         11        DO_ICALL                                                 
         12      > 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/2CmLp
function name:  json_is
number of ops:  12
compiled vars:  !0 = $string, !1 = $e
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    9     0  E >   RECV                                             !0      
   14     1        INIT_FCALL                                               'json_decode'
          2        SEND_VAR                                                 !0
          3        DO_ICALL                                                 
          4      > JMP                                                      ->7
   16     5  E > > CATCH                                       last         'ErrorException'
   19     6    > > RETURN                                                   <false>
   23     7    >   INIT_FCALL                                               'json_last_error'
          8        DO_ICALL                                         $3      
          9        IS_EQUAL                                         ~4      $3, 0
         10      > RETURN                                                   ~4
   24    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 = 9, Position 2 = 11
Branch analysis from position: 9
2 jumps found. (Code = 43) Position 1 = 12, Position 2 = 20
Branch analysis from position: 12
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
filename:       /in/2CmLp
function name:  isJson
number of ops:  22
compiled vars:  !0 = $string, !1 = $assoc
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   47     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      <false>
   50     2        INIT_FCALL                                               'json_decode'
          3        SEND_VAR                                                 !0
          4        SEND_VAR                                                 !1
          5        DO_ICALL                                                 
   52     6        ISSET_ISEMPTY_CV                                 ~3      !0
          7        BOOL_NOT                                         ~4      ~3
          8      > JMPZ_EX                                          ~4      ~4, ->11
          9    >   TYPE_CHECK                                   64  ~5      !0
         10        BOOL                                             ~4      ~5
         11    > > JMPZ                                                     ~4, ->20
   53    12    >   INIT_FCALL                                               'json_decode'
         13        SEND_VAR                                                 !0
         14        SEND_VAR                                                 !1
         15        DO_ICALL                                                 
   54    16        INIT_FCALL                                               'json_last_error'
         17        DO_ICALL                                         $7      
         18        IS_EQUAL                                         ~8      $7, 0
         19      > RETURN                                                   ~8
   58    20    > > RETURN                                                   <false>
   59    21*     > RETURN                                                   null

End of function isjson

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
151.93 ms | 1403 KiB | 22 Q