Home > Article > Backend Development > How to return error code in php
In PHP, you can use the header() function to return an error code, which means adding 404 header information to the page. The syntax is such as "header("HTTP/1.0 404 Not Found");".
The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer
How does php return an error code?
Using the header() function in PHP, you can add 404 header information to the returned page, thus prompting the browser that the web page cannot be found.
So you can use:
header("HTTP/1.0 404 Not Found");
or:
header("Status: 404 Not Found");
The latter is used in FastCGI mode. You can write the two sentences directly at the same time in the php code.
Extract the header usage instructions from the official website of the PHP manual as follows:
The header string.There are two special-case header calls. The first is a header that starts with the string "HTTP/" (case is not significant), which will be used to figure out the HTTP status code to send. For example, if you have configured Apache to use a PHP script to handle requests for missing files (using the ErrorDocument directive), you may want to make sure that your script generates the proper status code.For FastCGI you must use the following for a 404 response:
The header("Location: xxx.com"); defaults to a 302 status jump, so it cannot be browsed The controller outputs a 404 error status.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to return error code in php. For more information, please follow other related articles on the PHP Chinese website!