Home > Article > Backend Development > How to set status code in php
In PHP, you can use the http_response_code() function to set the status code. This function is used to set the HTTP status code of the response. The syntax format is "http_response_code (status code)".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php setting status code
http_response_code() function is used to get/set the HTTP status code of the response
Syntax:
http_response_code ($response_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:
<?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 status code in php. For more information, please follow other related articles on the PHP Chinese website!