What is the importance of setting the correct header when returning JSON data in PHP?

Setting the correct header when returning JSON data in PHP is important because it informs the client that the response is in JSON format. This ensures that the client knows how to handle the data properly. Without setting the correct header, the client may not be able to parse the JSON data correctly, leading to errors or unexpected behavior.

<?php
header('Content-Type: application/json');

$data = array('name' => 'John', 'age' => 30, 'city' => 'New York');
echo json_encode($data);
?>