Return JSON from PHP

A tiny helper for AJAX endpoints. Sends the right header and stops.

PHP · Return JSON from PHPBeginner
function json_out(array $data, int $status = 200): void
{
    http_response_code($status);
    header('Content-Type: application/json; charset=utf-8');
    echo json_encode($data, JSON_UNESCAPED_UNICODE);
    exit;
}

json_out(['ok' => true, 'message' => 'Saved']);
apijsonajax