The PHP programming language is used to create JSON objects. Let's begin by setting up the environment where we'll be programming in PHP for JSON.
The JSON extension is packed and compiled into PHP by default as PHP 5.2.0.
Function | Libraries |
---|---|
json encode | Returns a value's JSON representation. |
json_decode | This function decodes a JSON string. |
json_last_error | The last error that occurred is returned. |
JSON NUMERIC CHECK, JSON PRETTY PRINT, JSON UNESCAPED SLASHES, and JSON FORCE OBJECT are some of the JSON functions.
The example below demonstrates how to convert an array into JSON using PHP.
This will yield the following result while running: "a":1,"b":2,"c":3,"d":4","e":5"
The following example demonstrates how to convert PHP objects to JSON.
This will generate the following result while running: "name":"sachin","hobbies":"sports","birthdate":"08/05/1974 12:20:03 pm"
PHP's json decode function decodes JSON.
PHP In PHP, the json decode() function is used to decode JSON. This function converts the decoded json value to the proper PHP type.
json decode ($json [,$assoc = false [, $depth = 512 [, $options = 0 ]]) ($json [,$assoc = false [, $depth = 512 [, $options = 0 ]])
json string is an encoded string that must contain UTF-8 encoded data.
depth is an integer type parameter that indicates the depth of recursion options. JSON BIGINT AS STRING is an integer type bitmask that can be used to decode JSON.
The sample below demonstrates how PHP may be used to decode JSON objects.
While executing, it will produce the following result −
|