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) { 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/4Q492
function name:  (null)
number of ops:  13
compiled vars:  !0 = $a, !1 = $b, !2 = $c
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   63     0  E >   ASSIGN                                                   !0, <array>
   64     1        INIT_FCALL                                               'json_encode'
          2        SEND_VAR                                                 !0
          3        DO_ICALL                                         $4      
          4        ASSIGN                                                   !1, $4
   67     5        ASSIGN                                                   !2, ''
   69     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/4Q492
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 = 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/4Q492
function name:  isJson
number of ops:  18
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>
   48     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
   49     8    >   INIT_FCALL                                               'json_decode'
          9        SEND_VAR                                                 !0
         10        SEND_VAR                                                 !1
         11        DO_ICALL                                                 
   50    12        INIT_FCALL                                               'json_last_error'
         13        DO_ICALL                                         $6      
         14        IS_EQUAL                                         ~7      $6, 0
         15      > RETURN                                                   ~7
   54    16    > > RETURN                                                   <false>
   55    17*     > RETURN                                                   null

End of function isjson

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
154.83 ms | 1403 KiB | 22 Q