Home > Article > Backend Development > How to set http status code in php
php method to set http status code: first create a PHP sample file; then set the response HTTP status code through the "http_response_code(int $response_code = ?): mixed" method.
The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer
phpHow to set http status code
php method to set http status code
http_response_code - Get/set the HTTP status code of the response
Description
http_response_code(int $response_code = ?): mixed
Get or set the HTTP status code of the response status code.
Parameters
response_code
The optional response_code will set the status code of the response.
Return value
If response_code is provided, the previous status code will be returned. If response_code is not provided, the current status code will be returned. In a web server environment, the default value for these status codes is 200.
If called in a non-Web server environment (such as a CLI application), if response_code is not provided, false will be returned. In a non-web server environment, providing response_code will return true (only if the status code has not been previously set).
Example
Example using http_response_code() in a Web server environment
<?php // 获取当前状态码,并设置新的状态码 var_dump(http_response_code(404)); //获取新的状态码 var_dump(http_response_code()); ?>
The above routine will output:
int(200) int(404)
Recommended learning: "PHP Video tutorial》
The above is the detailed content of How to set http status code in php. For more information, please follow other related articles on the PHP Chinese website!