Q. Explain http_response_code() in php.
Answer
The http_response_code() function in PHP is used to get or set the HTTP response status code. It allows you to programmatically set the HTTP status code that will be sent in the response headers when your PHP script is executed. This function is particularly useful for signaling the status of the HTTP response, such as success (200 OK), redirection (e.g., 301 Moved Permanently), or various error conditions.
Here's the basic syntax:
int http_response_code([int $response_code])
If called without any arguments, http_response_code() returns the current HTTP response code.
If called with an argument (an integer representing the HTTP response code), it sets the response code for the current script.
Here's an example:
<?php
// Set the HTTP response code to 404 Not Found
http_response_code(404);
// Output a custom error message
echo 'Sorry, the page you requested could not be found.';
?>
In this example, when the script is executed, it will set the HTTP response code to 404 (Not Found) and output a custom error message. The server will send the appropriate headers with the response, indicating to the client that the requested resource was not found.
It's worth noting that if you are using PHP 5.4.0 or later, you can also use the following syntax to set the HTTP response code:
http_response_code(404);
This syntax provides a more convenient and readable way to set the response code directly without the need for a separate function call.
Using http_response_code() is preferable to manually setting headers with header(), as it helps maintain clean and readable code while ensuring consistency and accuracy in setting HTTP response codes.
js
Friday, December 15, 2023
Explain http_response_code() in php.
Subscribe to:
Post Comments (Atom)
-
ভাৰতৰ ৰাজনৈতিক দল অনুশীলনীৰ প্রশ্নোত্তৰ অতি চমু প্রশ্নোত্তৰ প্রশ্ন ১। একদলীয় শাসন ব্যৱস্থা থকা এখন দেশৰ নাম লিখা। উত্তৰঃ চীন। প...
-
উদ্যান শস্যৰ পৰিচয় পৰিচয় উদ্যান শস্য এটা বিজ্ঞান , লগতে , উদ্যান শস্য , যেনে ফল - মূল আৰু শাক - পাচলি , ...
No comments:
Post a Comment