PHP http_response_code() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Network

Definition

In PHP, http_response_code() gets or sets the HTTP response status code for the current request, such as 200, 404 or 302.

Call it with a number to set the status. Call it with no argument to read the current code. On SAPIs that cannot track a status, a get with no argument may return false.

Syntax

Call http_response_code() with the values you need. Start with this line.

PHP
$code = http_response_code(404);
CallWhat it does
http_response_code(404)Sets status 404 and returns the previous code.
http_response_code()Returns the current response code.
http_response_code(201)Sets Created for a successful create-style response.

Prefer this helper when you only need a status code. You can still use header() for Location and other fields. Setting a code returns the previous code; getting with no arguments returns the current code (or false when unavailable).

Library declarations
PHP
http_response_code(?int $response_code = null): int|bool

Parameters

ParameterDescription
response_codeOptional. The HTTP status code to set, such as 200 or 404. Omit it to read the current code.

Return Value

Return ValueDescription
int|boolWhen setting, returns the previous status code. When getting, returns the current code, or false if the status cannot be determined.

PHP Examples

Example 1 — Set and Read a Status Code

Set 404, then read the current code back for display.

PHP
<?php
http_response_code(404);
echo http_response_code(), PHP_EOL;

How It Works

  1. http_response_code(404) sets the Not Found status.
  2. Calling it again with no argument reads the current code.
  3. echo prints 404.
  4. On some CLI builds the get may differ; web SAPIs keep the value you set.

Example 2 — Previous Code When Setting

Show that setting a new code returns the previous value.

PHP
<?php
http_response_code(200);
$previous = http_response_code(201);
echo "previous=", $previous, PHP_EOL;
echo "current=", http_response_code(), PHP_EOL;

How It Works

  1. First we establish status 200.
  2. http_response_code(201) sets Created and returns the old 200.
  3. A bare http_response_code() call then reads 201.
  4. This helps logging when middleware changes the status.

Example 3 — Pair Status With a Header

Set 302 and queue a Location header, then list the header as a web SAPI would.

PHP
<?php
http_response_code(302);
header("Location: /login");

echo "status=", http_response_code(), PHP_EOL;
foreach (["Location: /login"] as $line) {
    echo $line, PHP_EOL;
}

How It Works

  1. http_response_code(302) sets the redirect status.
  2. header() adds the Location target.
  3. We print the status and an illustrative Location line.
  4. In production you would usually exit after a redirect.

Frequently Asked Questions

It gets or sets the HTTP response status code. Pass a number to set it, or call it with no argument to read the current code.
It returns the previous status code, then updates to the new one.
Some SAPIs, especially certain CLI setups, cannot report a response status, so a get with no argument returns false.
http_response_code(404) is simpler for most apps. Use header() when you also need other header fields.
Usually no. Once headers are sent, status changes are too late. Check headers_sent() first.

Next: inet_ntop()

inet_ntop() converts a packed binary IP address into the human-readable presentation form .

inet_ntop() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful