Challenge 1 — Solution Task: Write a jsonResponse() helper function (as shown in the chapter), then write a small script simulating a "user not found" scenario: it should call jsonResponse(['error' => 'User not found'], 404). Explain in a comment why setting the status code AND the Content-Type header both matter, even though the JSON body itself is valid either way. 'User not found'], 404); // Why both the status code and Content-Type header matter, even // though the JSON body itself is valid either way: // // The status code (404 here) is what a client application actually // checks PROGRAMMATICALLY to decide how to react - retry the // request, show a specific "not found" UI state, or treat it as a // genuine success. A client that only looked at the JSON body // would need to parse it first just to discover something went // wrong, which defeats much of the purpose of having status codes // in the first place (this chapter's own warn-box). Content-Type: // application/json matters separately - it tells the client HOW to // interpret the raw response bytes. Without it, some HTTP clients // might not automatically parse the body as JSON at all, treating // it as plain text instead - the body's own content being valid // JSON doesn't help if the client never attempts to parse it as // JSON to begin with. ?> Output: {"error":"User not found"} (sent with a real 404 HTTP status code and a Content-Type: application/json response header, both invisible in the raw echoed text above but genuinely present in the actual HTTP response) Notes: - http_response_code($statusCode) must be called before header() and echo, since HTTP status codes and headers are both part of the response's own header section, which must be sent before the body. - exit immediately after echoing the JSON prevents any further script output (e.g. from other error-handling code) from accidentally appending non-JSON text after the response body. - This exact jsonResponse() function is directly reused, unchanged, by the chapter's own PostApiController example - demonstrating it's a genuinely reusable helper, not tied to any one specific endpoint.