How to send HTTP response code in PHP? Last Updated : 11 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report HTTP Response code: There are three approaches to accomplish the above requirement, depending on the version. For PHP versions 4.0: In order to send the HTTP response code, we need to assemble the response code. To achieve this, use header() function. The header() function contains a special use-case which can detect a HTTP response line and replace that with a custom one. header("HTTP/1.1 200 OK"); Example: php $sapitype = php_sapi_name(); if (substr($sapitype, 0, 3) == 'cgi') header("Status: 404 Not Found"); else header("HTTP/1.1 404 Not Found"); For PHP versions 4.3: There are obviously a few problems when using first method. The biggest one is that it is partly parsed by PHP and is poorly documented. Since the version 4.3, the header() function has an additional 3rd argument through which we can set the response code. but to use the first argument should be a non-empty string. header(':', true, 400); header(‘X_PHP_Response_Code: 400', true, 400); The second one is recommended. The header field name in this variant is not standardized and can be modified. For PHP versions 5.4: This versions uses http_response_code() function to makes things easier. http_response_code(400); Example: This example uses http_response_code() function to send HTTP response code. php <?php error_reporting(E_ERROR | E_PARSE); // Initialize a variable into domain name $domain1 = 'https://write.geeksforgeeks.org/'; // Function to get HTTP response code function get_http_response_code($domain1) { $headers = get_headers($domain1); return substr($headers[0], 9, 3); } // Function call $get_http_response_code = get_http_response_code($domain1); // Display the HTTP response code echo $get_http_response_code; // Check HTTP response code is 200 or not if ( $get_http_response_code == 200 ) echo "<br>HTTP request successfully"; else echo "<br>HTTP request not successfully!"; ?> Output: 200 HTTP request successfully Comment More infoAdvertise with us Next Article How to add 301 redirects in PHP ? S shreyaraj1234 Follow Improve Article Tags : Web Technologies PHP PHP-Questions Similar Reads How to use PHP in HTML ? In this article, we will use PHP in HTML. There are various methods to integrate PHP and HTML, some of them are discussed below. You can add PHP tags to your HTML Page. You simply need to enclose the PHP code with the PHP starts tag <?php and the PHP end tag ?>. The code wrapped between these 2 min read HTTP status codes | Client Error Responses The browser and the site server have a conversation in the form of HTTP status codes. The server gives responses to the browserâs request in the form of a three-digit code known as HTTP status codes. The categorization of HTTP status codes is done in five sections which are listed below. Information 4 min read How to create and send POST requests in Postman? Postman is an API(application programming interface) development tool which helps to build, test and modify APIs. It can make various types of HTTP requests(GET, POST, PUT, PATCH), saving environments for later use, and convert save the API to code for various languages(like JavaScript, and Python). 2 min read How to add 301 redirects in PHP ? In this article, we are going to discuss how to add 301 redirects in PHP. Sometimes we come across scenarios like a few web pages redirecting from one page to another page without clicking/doing anything. This can be done by developers if they want users to use their updated version of their webpage 3 min read How to send a POST Request with PHP ? In web development, sending POST requests is a common practice for interacting with servers and exchanging data. PHP, a versatile server-side scripting language, provides various approaches to accomplish this task. This article will explore different methods to send POST requests using PHP. Table of 3 min read How to embed PHP code in an HTML page ? PHP is the abbreviation of Hypertext Preprocessor and earlier it was abbreviated as Personal Home Page. We can use PHP in HTML code by simply adding a PHP tag without doing any extra work. Example 1: First open the file in any editor and then write HTML code according to requirement. If we have to a 2 min read Like