You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
JSON_ERROR_NONE confirms whether error occurred or not
JSON_ERROR_SYNTAX indicates syntax error
JSON_ERROR_UTF8 encoding issues
JSON_FORCE_OBJECT outputs an object rather than an array when a non-associative array is used. Especially useful when the recipient of the output is expecting an object and the array is empty
JSON FUNCTIONS :
json_decode takes a JSON encoded string and converts it into a PHP variable.
json_decode returns the values encoded in json in appropriate PHP type. Values, false and null are returned as
true, false, and null respectively.
Null is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit.
<?php$json = '{"foo-bar": 12345}';
$obj = json_decode($json);
// can't access like this $obj->'foo-bar!
print $obj->{'foo-bar'}; // 12345
❗ common mistakes using json_decode
<?php/** * The following strings are a valid JAVASCRIPT objects BUT * the name and value must be enclosed in double quotes */$bad_json = "{'bar': 'baz'}";
json_decode($bad_json); // null$bad_json = "{bar: 'baz'}";
// the name must be enclosed in double quotesjson_decode($bad_json); // null$bad_json = '{"bar": "baz", }';
// trailing commas are not allowedjson_decode($bad_json); // null