Why is it advisable to set the Content-Type header to application/json when outputting JSON data in PHP?

Setting the Content-Type header to application/json when outputting JSON data in PHP is advisable because it informs the client that the response is in JSON format. This helps the client understand how to interpret the data correctly. Without setting the Content-Type header, the client may not know how to handle the response, leading to potential parsing errors.

<?php
$data = ['key' => 'value'];

header('Content-Type: application/json');
echo json_encode($data);