Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

http_response_code

Sets or returns the response status code.

Syntax

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

Parameters

ParameterTypeDescription
$response_codeintOptional. 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.

Thanks for reading!

Donate to the site — we work hard for you.