http_response_code
http_response_code(int $response_code = 0): int|bool
Sets or gets the HTTP response status code; under CLI getting returns false and setting is a no-op (the same Web/CLI SAPI split as setcookie).
C C23
#include <stdio.h>
int main(void) {
/* The lowest-level truth: the status line is literally the first bytes
of the response, handwritten in a minimal server */
printf("%s\r\n", "HTTP/1.1 404 Not Found");
printf("%s\r\n", "HTTP/1.1 301 Moved Permanently");
return 0;
}
HTTP/1.1 404 Not Found HTTP/1.1 301 Moved Permanently
C++ C++26
#include <iostream>
int main() {
// The status line is the first bytes on the wire; frameworks wrap it,
// but a minimal server writes it by hand
std::cout << "HTTP/1.1 404 Not Found\r\n";
std::cout << "HTTP/1.1 301 Moved Permanently\r\n";
return 0;
}
HTTP/1.1 404 Not Found HTTP/1.1 301 Moved Permanently
C# v11.0.100
// ASP.NET Core: results are typed values (Results.NotFound()); the
// status is data paired with the body. Offline wire-format sketch:
using System;
static string StatusLine(int code, string reason) => $"HTTP/1.1 {code} {reason}";
Console.WriteLine(StatusLine(404, "Not Found"));
Console.WriteLine(StatusLine(301, "Moved Permanently"));
HTTP/1.1 404 Not Found HTTP/1.1 301 Moved Permanently
Go v1.27.4
package main
import (
"fmt"
"net/http"
)
func main() {
// http.Error / WriteHeader set the status; WriteHeader must be called at
// most once per response (a second call is ignored)
fmt.Println(http.StatusText(404))
fmt.Println(http.StatusText(301))
fmt.Println(http.StatusText(200) != "")
}
Not Found Moved Permanently true
Java v27.0.1
// Servlet: response.setStatus(404); Spring: ResponseEntity.status(...).
// Offline wire-format sketch:
class Main {
static String statusLine(int code, String reason) {
return "HTTP/1.1 " + code + " " + reason;
}
public static void main(String[] args) {
System.out.println(statusLine(404, "Not Found"));
System.out.println(statusLine(301, "Moved Permanently"));
}
}
HTTP/1.1 404 Not Found HTTP/1.1 301 Moved Permanently
JavaScript v26.13.1
// Express: res.status(404); Node http: res.statusCode = 404 - the
// status line is written explicitly. Offline wire-format sketch:
function statusLine(code, reason) {
return `HTTP/1.1 ${code} ${reason}`;
}
console.log(statusLine(404, "Not Found"));
console.log(statusLine(301, "Moved Permanently"));
HTTP/1.1 404 Not Found HTTP/1.1 301 Moved Permanently
PHP v8.6.0
// Web SAPI: get returns the current code, set both sets and returns it.
// CLI SAPI: no HTTP response exists - get is false, set is a no-op
// (the same Web/CLI SAPI split as setcookie)
var_dump(http_response_code()); // false - nothing to read in CLI
var_dump(http_response_code(404)); // false - set is ignored outside Web SAPI
var_dump(http_response_code()); // still false
bool(false) bool(false) bool(false)
Python v3.15.2
from http.server import BaseHTTPRequestHandler
# Python's stdlib has no standalone "set status code" - servers write the
# status line explicitly (BaseHTTPRequestHandler.send_response);
# Flask passes status to jsonify. The reason phrases are stdlib constants:
print(f"HTTP/1.1 {404} {BaseHTTPRequestHandler.responses[404][0]}")
print(f"HTTP/1.1 {301} {BaseHTTPRequestHandler.responses[301][0]}")
HTTP/1.1 404 Not Found HTTP/1.1 301 Moved Permanently
Rust v1.99.1
// Rust web frameworks (axum/actix) pair a typed StatusCode with the
// body: the status is a value in a (StatusCode, Body) tuple, not a global
// side effect. Offline wire-format sketch with std only:
fn status_line(code: u16, reason: &str) -> String {
format!("HTTP/1.1 {} {}", code, reason)
}
fn main() {
println!("{}", status_line(404, "Not Found"));
println!("{}", status_line(301, "Moved Permanently"));
}
HTTP/1.1 404 Not Found HTTP/1.1 301 Moved Permanently
TypeScript v26.13.1
// Same wire format as JS; Express types the chain res.status(404).send(...)
function statusLine(code: number, reason: string): string {
return `HTTP/1.1 ${code} ${reason}`;
}
console.log(statusLine(404, "Not Found"));
console.log(statusLine(301, "Moved Permanently"));
HTTP/1.1 404 Not Found HTTP/1.1 301 Moved Permanently
Language differences
C: the status line is literally printf("HTTP/1.1 404 Not Found\\r\\n") — no abstraction at all; a complete minimal HTTP server (socket + handwritten headers) is networking lesson one.
C++: same handwritten status line as C; frameworks (Crow/Drogon's res.status = crow::status::NOT_FOUND etc.) wrap it as property assignment.
C#: ASP.NET Core models responses as typed values (Results.NotFound() / TypedResults), the status paired with the body — more functional than PHP's global side effect.
Go: w.WriteHeader(404) may be called at most once per response — a second call is ignored (classic pitfall: writing the body first leaves no room to set the status); http.Error is the one-shot "status + error body" shortcut; http.StatusText gives standard reason phrases.
Java: Servlet response.setStatus(404) (sendError(404) also serves an error page); Spring wraps it in typed ResponseEntity.status(...) / @ResponseStatus.
JavaScript: Express res.status(404) (chainable), bare Node res.statusCode = 404 / res.writeHead(404, headers); the status must be settled before the body is written.
PHP: the CLI vs Web SAPI split (example): on the Web, http_response_code() with no argument returns the current code and with an argument sets and returns it; in CLI the former is false and the latter is a no-op; quick reference of common codes: 200 OK / 201 Created / 301 & 302 redirects / 304 Not Modified / 400 Bad Request / 401 Unauthorized / 403 Forbidden / 404 Not Found / 422 Unprocessable / 500 Internal / 503 Unavailable — PHP ships no constants, just write the numbers.
Python: the stdlib layer writes the status line by hand (send_response also logs), the framework layer is Flask jsonify(..., status=404) / Django HttpResponse(status=404).
Rust: axum/actix make the status part of a (StatusCode, Body) tuple — status as data, not a side effect; http::StatusCode::NOT_FOUND is the typed constant.
TypeScript: same as JavaScript (Express chain + types); on the Fetch side Response.status is read-only.
— The CLI/Web SAPI split mirrors the setcookie entry: response-header operations only mean something when you are actually serving HTTP.