I have a PHP script that needs to make responses with HTTP response codes (status-codes), like HTTP 200 OK, or some 4XX or 5XX code.
How can I do this in PHP?
We can get different return value from http_response_code via the two different environment:
At the web server environment, return previous response code if you provided a response code or when you do not provide any response code then it will be print the current value. Default value is 200 (OK).
At CLI Environment, true will be return if you provided a response code and false if you do not provide any response_code.
Example of Web Server Environment of Response_code's return value:
var_dump(http_respone_code(500)); // int(200)
var_dump(http_response_code()); // int(500)
Example of CLI Environment of Response_code's return value:
var_dump(http_response_code()); // bool(false)
var_dump(http_response_code(501)); // bool(true)
var_dump(http_response_code()); // int(501)