Skip to content

PHP Tutorial Working with JSON and APIs

HTTP Status Codes

Lesson 309 of 369 3 min read By Deepak Paswan Updated Oct 5, 2026

Every HTTP response starts with a status code: a three-digit number that tells the client what happened. Browsers, search engines and API clients all rely on it.

The five groups

RangeMeaning
1xxInformation (rarely used directly)
2xxSuccess
3xxRedirection
4xxThe client made a mistake
5xxThe server failed

The codes you will use

CodeNameUse it when
200OKThe request worked (the default)
201CreatedA resource was created (after POST)
204No ContentSuccess with nothing to send back (after DELETE)
301Moved PermanentlyA page has a new permanent address
302 / 303Found / See OtherA temporary redirect (after a form post)
304Not ModifiedThe cached copy is still good
400Bad RequestThe request is malformed or invalid
401UnauthorizedThe user is not logged in (authentication needed)
403ForbiddenLogged in, but not allowed
404Not FoundThe resource does not exist
405Method Not AllowedWrong HTTP method for this URL
409ConflictThe request clashes with the current state (duplicate)
415Unsupported Media TypeWrong Content-Type
422Unprocessable ContentValid syntax, but the data fails validation
429Too Many RequestsRate limit reached
500Internal Server ErrorSomething broke on the server
503Service UnavailableMaintenance or overload

Sending a status code in PHP

index.php
PHP
<?php
http_response_code(404);
echo "Page not found";

Call it before output, and remember that the code is part of the headers.

index.php
PHP
<?php
http_response_code(201);
header("Location: /products/42");   // where the new resource is

For redirects, header("Location: ...") sets 302 automatically. To choose, pass the code as the third argument: header("Location: /new", true, 301).

Why correct codes matter

  • Search engines. A missing page that returns 200 (a “soft 404”) confuses search engines. A moved page should return 301.
  • Clients. A JavaScript fetch() treats 404 and 500 differently from 200, and can react.
  • Monitoring. Tools count 5xx responses to find outages.

Choose between 401, 403 and 404

index.php
PHP
<?php
function statusFor(bool $loggedIn, bool $allowed, bool $exists): int
{
    if (!$loggedIn) {
        return 401;
    }

    if (!$exists) {
        return 404;
    }

    return $allowed ? 200 : 403;
}

echo statusFor(false, false, true) . " ";
echo statusFor(true, false, true) . " ";
echo statusFor(true, true, false) . " ";
echo statusFor(true, true, true);
Output
OUTPUT
401 403 404 200

Sometimes you return 404 instead of 403 on purpose, so that people cannot find out which private resources exist.

400 vs 422

  • 400: the request itself is broken (invalid JSON, missing required header).
  • 422: the JSON is fine, but a value is not acceptable (an invalid email). Many APIs simply use 400 for both. Be consistent.

Names for codes

index.php
PHP
<?php
$names = [200 => "OK", 201 => "Created", 404 => "Not Found", 500 => "Internal Server Error"];

foreach ([201, 404] as $code) {
    echo "$code {$names[$code]}\n";
}
Output
OUTPUT
201 Created
404 Not Found

Read a status code

When you call another API, check the status before you trust the body. API Error Handling shows how.

lightbulb
A rule of thumb
2xx means “it worked”, 4xx means “you sent something wrong, do not retry unchanged”, 5xx means “we failed, you can retry later”.