http_response_code
Sets or returns the response status code.
Syntax
http_response_code(int $response_code = 0): int|boolParameters
| Parameter | Type | Description |
|---|---|---|
| $response_code | int | Optional. The new code. |
Return value
int|boolThe previous code, or the current one when no argument is given.
Examples
A "not found" JSON response:
<?php
http_response_code(404);
header('Content-Type: application/json');
echo json_encode(['error' => 'Not found']);Notes
Set the code before any output. The most common: 200, 201, 301, 302, 400, 401, 403, 404, 422, 500.
See also
- header — Sends an HTTP response header.
- json_encode — Converts a value to a JSON string.
- session_start — Starts a session or resumes the existing one.
Related recipes
In the tutorial
- HTTP requests and responses — How PHP receives a request and builds a response — the request method and address, headers, status codes, redirects and JSON responses.