WEB TUTORIALS
PRACTICE EXAMPLES
HTML REFERENCES
CSS REFERENCES
PHP REFERENCES

How to Return JSON from a PHP Script

Topic: PHP / MySQLPrev|Next

Answer: Use the json_encode() Function

You can simply use the json_encode() function to return JSON response from a PHP script. Also, if you're passing JSON data to a JavaScript program, make sure set the Content-Type header.

Let's take a look at an example to understand how it basically works:

<?php
// Sample array
$data = array("a" => "Apple", "b" => "Ball", "c" => "Cat");

header("Content-Type: application/json");
echo json_encode($data);
exit();
?>

Related FAQ

Here are some more FAQ related to this topic:

Bootstrap UI Design Templates Property Marvels - A Leading Real Estate Portal for Premium Properties