Q:

How to Return JSON from a PHP Script

0

How to Return JSON from a PHP Script

All Answers

need an explanation for this answer? contact us directly to get an explanation for this 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();
?>

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now