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 is_json($string) { return !empty($string) && is_string($string) && is_array(json_decode($string, true)) && json_last_error() == 0; } //I think in PHP it's more important to determine if the JSON object even has data, because to use the data you will need to call json_encode() //or json_decode(). I suggest denying empty JSON objects so you aren't unnecessarily running encodes and decodes on empty data. function has_json_data($string) { $array = json_decode($string, true); return !empty($string) && is_string($string) && is_array($array) && !empty($array) && json_last_error() == 0; } function isJson($string, $assoc = false) { var_dump(empty(json_decode($string))); 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/0VaRK
function name:  (null)
number of ops:  13
compiled vars:  !0 = $a, !1 = $b, !2 = $c
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   83     0  E >   ASSIGN                                                   !0, <array>
   84     1        INIT_FCALL                                               'json_encode'
          2        SEND_VAR                                                 !0
          3        DO_ICALL                                         $4      
          4        ASSIGN                                                   !1, $4
   87     5        ASSIGN                                                   !2, '%7B%7D'
   89     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/0VaRK
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 is_json:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 46) Position 1 = 4, Position 2 = 6
Branch analysis from position: 4
2 jumps found. (Code = 46) Position 1 = 7, Position 2 = 13
Branch analysis from position: 7
2 jumps found. (Code = 46) Position 1 = 14, Position 2 = 18
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 18
Branch analysis from position: 13
Branch analysis from position: 6
filename:       /in/0VaRK
function name:  is_json
number of ops:  20
compiled vars:  !0 = $string
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   48     0  E >   RECV                                             !0      
   49     1        ISSET_ISEMPTY_CV                                 ~1      !0
          2        BOOL_NOT                                         ~2      ~1
          3      > JMPZ_EX                                          ~2      ~2, ->6
          4    >   TYPE_CHECK                                   64  ~3      !0
          5        BOOL                                             ~2      ~3
          6    > > JMPZ_EX                                          ~2      ~2, ->13
          7    >   INIT_FCALL                                               'json_decode'
          8        SEND_VAR                                                 !0
          9        SEND_VAL                                                 <true>
         10        DO_ICALL                                         $4      
         11        TYPE_CHECK                                  128  ~5      $4
         12        BOOL                                             ~2      ~5
         13    > > JMPZ_EX                                          ~2      ~2, ->18
         14    >   INIT_FCALL                                               'json_last_error'
         15        DO_ICALL                                         $6      
         16        IS_EQUAL                                         ~7      $6, 0
         17        BOOL                                             ~2      ~7
         18    > > RETURN                                                   ~2
   50    19*     > RETURN                                                   null

End of function is_json

Function has_json_data:
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 = 46) Position 1 = 12, Position 2 = 14
Branch analysis from position: 12
2 jumps found. (Code = 46) Position 1 = 15, Position 2 = 18
Branch analysis from position: 15
2 jumps found. (Code = 46) Position 1 = 19, Position 2 = 23
Branch analysis from position: 19
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 23
Branch analysis from position: 18
Branch analysis from position: 14
Branch analysis from position: 11
filename:       /in/0VaRK
function name:  has_json_data
number of ops:  25
compiled vars:  !0 = $string, !1 = $array
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   54     0  E >   RECV                                             !0      
   55     1        INIT_FCALL                                               'json_decode'
          2        SEND_VAR                                                 !0
          3        SEND_VAL                                                 <true>
          4        DO_ICALL                                         $2      
          5        ASSIGN                                                   !1, $2
   56     6        ISSET_ISEMPTY_CV                                 ~4      !0
          7        BOOL_NOT                                         ~5      ~4
          8      > JMPZ_EX                                          ~5      ~5, ->11
          9    >   TYPE_CHECK                                   64  ~6      !0
         10        BOOL                                             ~5      ~6
         11    > > JMPZ_EX                                          ~5      ~5, ->14
         12    >   TYPE_CHECK                                  128  ~7      !1
         13        BOOL                                             ~5      ~7
         14    > > JMPZ_EX                                          ~5      ~5, ->18
         15    >   ISSET_ISEMPTY_CV                                 ~8      !1
         16        BOOL_NOT                                         ~9      ~8
         17        BOOL                                             ~5      ~9
         18    > > JMPZ_EX                                          ~5      ~5, ->23
         19    >   INIT_FCALL                                               'json_last_error'
         20        DO_ICALL                                         $10     
         21        IS_EQUAL                                         ~11     $10, 0
         22        BOOL                                             ~5      ~11
         23    > > RETURN                                                   ~5
   57    24*     > RETURN                                                   null

End of function has_json_data

Function isjson:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 46) Position 1 = 16, Position 2 = 18
Branch analysis from position: 16
2 jumps found. (Code = 43) Position 1 = 19, Position 2 = 27
Branch analysis from position: 19
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: 18
filename:       /in/0VaRK
function name:  isJson
number of ops:  29
compiled vars:  !0 = $string, !1 = $assoc
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   62     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      <false>
   65     2        INIT_FCALL                                               'var_dump'
          3        INIT_FCALL                                               'json_decode'
          4        SEND_VAR                                                 !0
          5        DO_ICALL                                         $2      
          6        BOOL_NOT                                         ~3      $2
          7        SEND_VAL                                                 ~3
          8        DO_ICALL                                                 
   67     9        INIT_FCALL                                               'json_decode'
         10        SEND_VAR                                                 !0
         11        SEND_VAR                                                 !1
         12        DO_ICALL                                                 
   69    13        ISSET_ISEMPTY_CV                                 ~6      !0
         14        BOOL_NOT                                         ~7      ~6
         15      > JMPZ_EX                                          ~7      ~7, ->18
         16    >   TYPE_CHECK                                   64  ~8      !0
         17        BOOL                                             ~7      ~8
         18    > > JMPZ                                                     ~7, ->27
   70    19    >   INIT_FCALL                                               'json_decode'
         20        SEND_VAR                                                 !0
         21        SEND_VAR                                                 !1
         22        DO_ICALL                                                 
   71    23        INIT_FCALL                                               'json_last_error'
         24        DO_ICALL                                         $10     
         25        IS_EQUAL                                         ~11     $10, 0
         26      > RETURN                                                   ~11
   74    27    > > RETURN                                                   <false>
   75    28*     > RETURN                                                   null

End of function isjson

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
157.12 ms | 1407 KiB | 22 Q