Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Web and Security

PHP http_response_code() function

Reference for PHP http_response_code(): set the HTTP status (404, 500, 301...), read the current code, how it differs from header() and what it returns in the CLI.

http_response_code() sets or reads the HTTP status code of the current response, the number such as 200, 404 or 500 that tells browsers, search engines and API clients how the request went. PHP sends 200 unless you change it. A Location header also changes it to 302.

It is the clearest way to say "not found" or "forbidden" without writing a full HTTP/1.1 404 Not Found line yourself, because PHP fills in the reason phrase for the protocol version in use.

PHP
http_response_code(404);
echo 'Page not found';

Try it

In a command-line run there is no web server, so the function behaves a little differently from a real request. These examples show that behavior and a safe wrapper you can reuse in web code.

PHP Reading and setting the code (CLI behavior)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP A helper that maps errors to status codes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Syntax

PHP
http_response_code(int $response_code = 0): int|bool
Name Description
$response_code Optional. The status code to send, normally from 100 to 599. If omitted or 0, the function only reads the current code.

Return value:

  • With no argument: the current code, or false if PHP is not running under a web server (CLI).
  • With an argument: the previous code, or true if none was set before.
  • If headers were already sent, the call has no effect, raises a warning and returns false.

Errors / Exceptions: none are thrown. Calling it after output has started raises a warning and changes nothing.

Usage notes

  • Call it before any output, just like header().
  • The page body is still sent. Setting 404 does not stop the script. Print your error page, then exit.
  • Use real status codes for errors. A "not found" page returned with 200 (a soft 404) confuses search engines and API clients.
  • Common codes: 200 OK, 201 Created, 204 No Content, 301 and 308 permanent redirects, 302 and 303 temporary redirects, 304 Not Modified, 400 Bad Request, 401 unauthenticated, 403 forbidden, 404 not found, 405 wrong method, 422 validation failed, 429 too many requests, 500 server error, 503 unavailable.
  • Redirects need a Location header as well: header('Location: /new', true, 301) sets both at once.
  • Alternative: header('HTTP/1.1 404 Not Found') works too, but ties your code to a protocol version. Prefer this function.
  • Under HTTP/2 and HTTP/3 there is no reason phrase; only the numeric code matters.
  • Frameworks let you set the status on a response object. The function is for plain scripts.