3v4l.org

run code in 300+ PHP versions simultaneously
<?php //You must validate your input to make sure the string you pass 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(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='{ "book": [ { "id":"01", "language": "Java", "edition": "third", "author": "Herbert Schildt" }, { "id":"07", "language": "C++", "edition": "second" "author": "E.Balagurusamy" }] }'; var_dump(isJson($c));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/BXLd3
function name:  (null)
number of ops:  13
compiled vars:  !0 = $a, !1 = $b, !2 = $c
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   38     0  E >   ASSIGN                                                   !0, <array>
   39     1        INIT_FCALL                                               'json_encode'
          2        SEND_VAR                                                 !0
          3        DO_ICALL                                         $4      
          4        ASSIGN                                                   !1, $4
   42     5        ASSIGN                                                   !2, '%7B%0A++++%22book%22%3A+%5B%0A++++%7B%0A+++++++%22id%22%3A%2201%22%2C%0A+++++++%22language%22%3A+%22Java%22%2C%0A+++++++%22edition%22%3A+%22third%22%2C%0A+++++++%22author%22%3A+%22Herbert+Schildt%22%0A++++%7D%2C%0A++++%7B%0A+++++++%22id%22%3A%2207%22%2C%0A+++++++%22language%22%3A+%22C%2B%2B%22%2C%0A+++++++%22edition%22%3A+%22second%22%0A+++++++%22author%22%3A+%22E.Balagurusamy%22%0A++++%7D%5D%0A%7D'
   58     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 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/BXLd3
function name:  is_json
number of ops:  20
compiled vars:  !0 = $string
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    7     0  E >   RECV                                             !0      
    8     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
    9    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/BXLd3
function name:  has_json_data
number of ops:  25
compiled vars:  !0 = $string, !1 = $array
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   13     0  E >   RECV                                             !0      
   14     1        INIT_FCALL                                               'json_decode'
          2        SEND_VAR                                                 !0
          3        SEND_VAL                                                 <true>
          4        DO_ICALL                                         $2      
          5        ASSIGN                                                   !1, $2
   15     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
   16    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 = 15, Position 2 = 17
Branch analysis from position: 15
2 jumps found. (Code = 43) Position 1 = 18, Position 2 = 26
Branch analysis from position: 18
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 26
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 17
filename:       /in/BXLd3
function name:  isJson
number of ops:  28
compiled vars:  !0 = $string, !1 = $assoc
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   21     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      <false>
   24     2        INIT_FCALL                                               'var_dump'
          3        INIT_FCALL                                               'json_decode'
          4        SEND_VAR                                                 !0
          5        DO_ICALL                                         $2      
          6        SEND_VAR                                                 $2
          7        DO_ICALL                                                 
   26     8        INIT_FCALL                                               'json_decode'
          9        SEND_VAR                                                 !0
         10        SEND_VAR                                                 !1
         11        DO_ICALL                                                 
   28    12        ISSET_ISEMPTY_CV                                 ~5      !0
         13        BOOL_NOT                                         ~6      ~5
         14      > JMPZ_EX                                          ~6      ~6, ->17
         15    >   TYPE_CHECK                                   64  ~7      !0
         16        BOOL                                             ~6      ~7
         17    > > JMPZ                                                     ~6, ->26
   29    18    >   INIT_FCALL                                               'json_decode'
         19        SEND_VAR                                                 !0
         20        SEND_VAR                                                 !1
         21        DO_ICALL                                                 
   30    22        INIT_FCALL                                               'json_last_error'
         23        DO_ICALL                                         $9      
         24        IS_EQUAL                                         ~10     $9, 0
         25      > RETURN                                                   ~10
   33    26    > > RETURN                                                   <false>
   34    27*     > RETURN                                                   null

End of function isjson

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
159.5 ms | 1406 KiB | 22 Q