TheLearner
TheLearner

Reputation: 2873

PHP environment constants: SITE_HTMLROOT vs. DOCUMENT_ROOT

I ran a test and both $_SERVER["SITE_HTMLROOT"] and $_SERVER["DOCUMENT_ROOT"] seem to return the same path. Is there any situation they could differ and if not, is there any consideration around picking one over the other?

Upvotes: 0

Views: 278

Answers (1)

axiac
axiac

Reputation: 72336

As the documentation of $_SERVER[] explains:

$_SERVER is an array containing information such as headers, paths, and script locations. The entries in this array are created by the web server. There is no guarantee that every web server will provide any of these; servers may omit some, or provide others not listed here. That said, a large number of these variables are accounted for in the » CGI/1.1 specification, so you should be able to expect those.

Also note that the values whose key names start with HTTP_ are extracted from the HTTP request headers and they will be missing if the request doesn't contain the corresponding headers.

Regarding your question:

Is there any situation they could differ and if not, is there any consideration around picking one over the other?

As @ÁlvaroGonzález explains in their comment, $_SERVER["SITE_HTMLROOT"] is provided by MediaTemple, which is a web hosting company.
It's obvious that using $_SERVER["SITE_HTMLROOT"] in the code will prevent it run in another environment that doesn't provide the value.

Also, I don't recommend using $_SERVER["DOCUMENT_ROOT"] either. Even if it probably works across most of the environments you could reach, it's better (and easier) to write your code to work without caring where on the disk it is located. All it needs is inside the project directory; you can get the paths of various project files and directories at runtime using the magic constant __FILE__, the function dirname() and strings concatenation.

Upvotes: 1

Related Questions