Are there any best practices for outputting JSON in PHP to ensure clean and efficient API responses?
When outputting JSON in PHP for API responses, it is important to ensure that the data is properly formatted and efficient. One best practice is to use the `json_encode()` function to convert PHP data structures into JSON format. Additionally, setting the appropriate headers using `header('Content-Type: application/json')` can help indicate to the client that the response is in JSON format.
// Sample code to output JSON response in PHP
$data = array(
'name' => 'John Doe',
'age' => 30,
'email' => 'john.doe@example.com'
);
header('Content-Type: application/json');
echo json_encode($data);