Content · Web Development · PHP

PHP http_response_code() Function: Usage and Examples

Reads or changes the current HTTP response status code.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

In PHP, http_response_code() helps with runtime and application data. Reads or changes the current HTTP response status code.

What Is http_response_code() in PHP?


The function or method has a specific purpose: Reads or changes the current HTTP response status code. The following examples show the main call, one additional situation, and an application-style use.

Syntax:

<?php
http_response_code(int $response_code = ...) : int|bool

Parameters and Return Value


  • $response_code (int) — argument used by this API to perform its operation; optional.

Example 1: Basic Usage


Reads or changes the current HTTP response status code.

<?php
http_response_code(201);
echo http_response_code();

This example demonstrates the central operation. The final output depends on the supplied input and, where relevant, the runtime or connected service.

Example 2: Handling a Different Case


<?php
http_response_code(404);
echo "Resource not found";

The second example focuses on a different input or failure condition, so the behavior can be distinguished from the basic case.

Practical Example


<?php
http_response_code(422);
header("Content-Type: application/json; charset=utf-8");
echo json_encode(["error" => "Invalid request"], JSON_THROW_ON_ERROR);

The practical example demonstrates how the same subject fits into a larger application. Use your own file paths, endpoints and validated inputs rather than copying sample settings directly.

Important Behavior and Edge Cases


  • Reads or changes the current HTTP response status code.
  • Check the return value and exceptions for failure before using the result. If an extension or external system is required, the example only works when that dependency is available. Network outcomes are not deterministic and responses should not be assumed to be successful.
  • A response status, media type and headers are part of the HTTP contract; do not expose sensitive details in error bodies.

Comparison With Related Functions


ApproachDifference
http_response_codeReads or changes the current HTTP response status code.
function_existsChecks that a PHP function name is available; does not perform this operation.

Common Beginner Mistakes


MistakeCorrect Approach
Ignoring errors from http_response_codeCheck the actual result and handle the expected error path.
Using the demonstration unchanged in productionAdapt file paths, configuration, authentication and permission rules to the application.
Treating input or returned data as trustedApply validation at input boundaries and context-aware escaping at output boundaries.

Key Takeaways


  • Reads or changes the current HTTP response status code.
  • Check the return value and exceptions for failure before using the result. If an extension or external system is required, the example only works when that dependency is available. Network outcomes are not deterministic and responses should not be assumed to be successful.
  • A response status, media type and headers are part of the HTTP contract; do not expose sensitive details in error bodies.

Reads or changes the current HTTP response status code. Use this approach when its capabilities and constraints match the application. Handle invalid data, failure conditions and resource cleanup explicitly.

Share this resourceSend this page to someone who may find it useful.