go to bug id or search bugs for
The manual page for the http_response_code function states:
"FALSE will be returned if response_code is not provided and it is not invoked in a web server environment (such as from a CLI application)."
Which is the way this behaves when http_response_code() has not been called already. However, if you call http_response_code() and pass it a value, further calls to http_response_code() without a value will return the value you've set even from a CLI environment.
This side effect could be a issue with any code that is written to work when called from any environment using this function to determine environment type. This issue could instantly be fixed by rewording the manual to more accurately describe the behavior of this function. IMHO PHP really *SHOULD* provide a definitive way to determine if we should be rendering HTML or plain text and, if this worked as advertised, it would fit that use case perfectly well. As it is, it does work but only if you can guarantee that nothing else is mucking about with the response code. This makes it unreliable to use in situations where the response code may have been set.
This problem is easily worked around by making sure that you store the value of http_response_code() before you call it with any parameters. Still, this is not the expected behavior given the wording of the manual and it also makes this function less useful as a simple check to determine if the script is being ran from a CLI environment.
I've verified that this is the case on PHP version 5.6.29 and is still a bug in version 7.2.2 . I have not checked any other versions.
// $is_cli is properly set to FALSE even though the response code defaults
// to 200 in all environments. However, this will no longer work after the
// next call to http_response_code().
// Calling http_response_code() with a status code causes the bug to occur.
// This is the bug. $is_cli should be FALSE if this is being ran from the CLI.
// In a web environment, it should now be 300.
Expected result in a CLI environment:
Expected result in a web environment:
Actual result in a CLI environment:
Actual result in a web environment:
Add a Patch
Add a Pull Request
The following pull request has been associated:
Patch Name: Fixes for #77841
On GitHub: https://github.com/php/php-src/pull/4131