http_response_code()

http_response_code() — Get or Set the HTTP response code.

Built-in function of PHP 8.5. The manual lists it as available in PHP 5 >= 5.4.0, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.

Signature

http_response_code(int $response_code = 0): int|bool

Parameters

ParameterTypeWhat it takes
$response_codeintThe optional response_code will set the response code.

Return value

If response_code is provided, then the previous status code will be returned. If response_code is not provided, then the current status code will be returned. Both of these values will default to a 200 status code if used in a web server environment. false will be returned if response_code is not provided and it is not invoked in a web server environment (such as from a CLI application). true will be returned if response_code is provided and it is not invoked in a web server environment (but only when no previous response status has been set).

Example

A runnable snippet:

// Get the current response code and set a new one var_dump(http_response_code(404)); // Get the new response code var_dump(http_response_code());

Output:

int(200) int(404)

See also

header(), headers_list()

All PHP 8.5 built-in functions

Reference card built from the PHP manual page http_response_code() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.