All TalkersCode Topics

Follow TalkersCode On Social Media

PHP json_decode Example

Last Updated : Jan 1, 2023

PHP json_decode Example

In this tutorial we will show you the solution of PHP json_decode example, json_decode is a built-in feature of PHP that converts json_encoded data to PHP data.

Shortly, we can say that json_decode is used to decode json_encoded object to PHP object.

Now, here below an example is given which helps to understand this json_decode in a better way.

Step By Step Guide On PHP json_decode Example :-

Now, before understanding the example given below. Let us see the syntax of json_decode. The syntax is as:

json_decode(string, assoc, depth, options)
  • String: string is a required parameter of the json_decode function, which helps to specify the values which we have to decode.
  • Whereas remaining assoc, depth, and options are optional.
<!DOCTYPE html>
   <title> PHP json_decode example </title>
 PHP json_decode example
$json = '{"Robert":99,"Thanos":60,"Steve":84}';
$data = '{"number": 123456789}';
$result = json_decode($data);
print $result ->{number};
  1. As, here we see that in the above example, we show you how HTML and PHP codes are used.
  2. Here, first of all, we create a basic structure of HTML, in which we use <!DOCTYPE html> which defines the type of document. And next one is our HTML tags. These tags are paired tags and all the data regarding HTML is written inside these tags.
  3. After we use our head tag which is again paired tag and contains the title and meta information of the webpage. The data written inside the head is not shown on the webpage.
  4. Now, next is our title tag which defines the title of the webpage. The tag which has its closing tag is known as a paired tag. So, this is again a paired tag.
  5. Now, next is the body which is the main tag of HTML. The data which we have written inside the body is shown on the webpage. Mostly all tags which are helpful to show data or information on the screen are written under the body tag.
  6. Now, as we see above are two different examples given in PHP. let us understand our example. As we see above in our first example, we see we store JSON encoded data inside a variable. And to display data we use JSON decode. And to show data we just dump this function and see our required output.
  7. Whereas in our next example, we again store our JSON data inside a variable. And to decode this data we use again json_decode function but does not display using dump directly. We just store data inside a variable. And display using the variable in which we store encoded data and calls JSON encoded variable.

Conclusion :-

At last, in conclusion, here we can say that with the help of this article we can understand json_decode with help of an example.

I hope this tutorial on php json_decode example helps you and the steps and method mentioned above are easy to follow and implement.

Latest Tutorials