|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #77841 http_response_code returns the HTTP response code from the CLI
Submitted: 2019-04-03 21:28 UTC Modified: -
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: krowe dot dev at gmail dot com Assigned:
Status: Open Package: HTTP related
PHP Version: Irrelevant OS: ALL
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: krowe dot dev at gmail dot com
New email:
PHP Version: OS:


 [2019-04-03 21:28 UTC] krowe dot dev at gmail dot com
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.

Test script:

// $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:
Expected result in a CLI environment:


Expected result in a web environment:


Actual result:
Actual result in a CLI environment:


Actual result in a web environment:



Add a Patch

Pull Requests

Pull requests:

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2019-05-08 17:14 UTC]
The following pull request has been associated:

Patch Name: Fixes for #77841
On GitHub:
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Sat Feb 04 18:03:44 2023 UTC