PHP Loves JSON

Javascript,PHP Programming 19 May 2007 View Comments

JSON is a format for communication between the server-side (PHP, JSP, ASP, etc…) and the client-side (javascript). The magic of it is that the response from the server-side can be easily converted to an object via the use of the eval() function. eval() (can be “evil”) is a function that gives you the possibility to execute some code in javascript from a string. To use eval() is not a good idea.

How can you JSON from PHP?

If you are using PHP 5.2.x and above, you can simply use PHP built-in function, json_encode() and json_decode(). This is an example how it works:

And it will echo :

[{"id":1,"name":"sumardi"},{"id":2,"name":"hassan"},{"id":3,"name":"ruby"}]

How to JSON on Client-Side?

Use the eval() function on the Server-Side JSON text response. And then you can call JSON like this:

blog comments powered by Disqus