Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

PHP Network http_response_code() Function



The PHP Network http_response_code() function is used to get or set a web server's HTTP response status code. This function plays an essential role for handling HTTP replies in your applications. You can change the response status code or get the previously defined code by giving a numeric code, like http_response_code(404).

If no code is given, it just returns the current response code, which is 200 in a web server environment. When used outside of a web server, like from the command line, its behavior changes: without a code, it returns false; with a code, it sets the status code and returns true.

Syntax

Below is the syntax of the PHP Network http_response_code() function −

int http_response_code ( int $response_code )

Parameters

This function accepts $response_code parameter which is the optional response_code will set the response code.

Return Value

If response_code is specified, the previous status code will be returned. If response_code is not specified, the current status code will be returned. When used in a web server environment, each of these values will return a 200 status code.

If no response_code is provided and the function is not called in a web server environment (for example, from a CLI application), it will return false. If response_code is provided but not executed in a web server context, true will be returned.

PHP Version

The http_response_code() function works with PHP versions 5.4.0 and up, including PHP 7 and 8.

Example 1

Here is the basic example of the PHP Network http_response_code() function to get or set the HTTP response status code. So it basically changes the HTTP response code to 404, which is commonly used to indicate that the requested resource is not available on the server.

<?php
   var_dump(http_response_code());  
   http_response_code(404);
?>

Output

Here is the outcome of the following code −

bool(false)

Example 2

In the below PHP code we will try to use the http_response_code() function and set a custom HTTP response code (404) for a 'Not Found' error. It helps in handling specific HTTP status for errors.

<?php
   // Set custom HTTP response code
   http_response_code(404); 

   // Display the response code
   echo "Response Code: " . http_response_code(); 
?> 

Output

This will generate the below output −

Response Code: 404

Example 3

The program checks whether it is running in a CLI or web server environment. When used in a command-line interface, it returns true; in a web server, it sets and then outputs the HTTP response code (404). It shows how the function behaves differently in various situations when you use http_response_code() function.

<?php
   // Set response code to 404 in web server environment
   if (php_sapi_name() == "cli") {
      // In CLI, returns true
      echo http_response_code(404); 
   } else {
      // In a web server, sets response code to 404
      http_response_code(404); 
      echo "HTTP Response Code: " . http_response_code(); 
   }
?> 

Output

In CLI (Command Line Interface), this will the output −

1

In Web Server, this will the output −

HTTP Response Code: 404
php_function_reference.htm
Advertisements