In this page, you will learn about installing JSON in PHP and about PHP json_decode() function with real world examples.
PHP 5.2.0 supports JSON out of the box. So, when you install PHP, JSON support is automatically installed and you don't need any installation and configuration in addition.
If your PHP version is less that 5.2.0, you can run the following command on RHEL5/CentOS5 Linux.
sudo yum install php-pecl-json
JSON supports three PHP functions : json_decode, json_encode and json_last_error. We will discuss all of these functions with examples in the following section of this page.
json_decode() function decodes a JSON string. Suppose you have obtained some data in JSON format and you want to convert it into PHP variable for the purpose of presenting that data to a user or use it for further programming, you have to use this function.
PHP 5 >= 5.2.0, PECL json >= 1.2.0
json_decode(json_string, assoc, depth, options)
|json_string||String||A JSON encoded string. It must be a UTF-8 encoded data.|
|assoc||Boolean||If it is true, returned object will be converted into an associative array upon using json_decode function.|
|depth||Integer||Specifies recursion depth. This is user specified.|
|Options||Integer||Bitmask of JSON decode. As of this writing, only JSON_BIGINT_AS_STRING is supported.|
json_decode() function returns an supported PHP type. If the available JSON string can not be decoded or if the encoded data is deeper than the recursion limit, it returns NULL. Values true and false are returned as TRUE, FALSE.
Note : As far as you are connected to the internet, you can run the above code from localhost also. Replace w3resource with the user name of your choice.
Solve these problems: