PHP: How to send HTTP response code?

后端 未结 9 1832
无人共我
无人共我 2020-11-22 06:05

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?

9条回答
  •  隐瞒了意图╮
    2020-11-22 06:32

    We can get different return value from http_response_code via the two different environment:

    1. Web Server Environment
    2. CLI 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)
    

提交回复
热议问题