PHP: Using predefined variables in paths
Predefined variables can be used to replace specific parts of the paths of files or images.
Predefined variable in a path for an include file
It’s important to understand that the path for an include file is relative to the web server and not the web root where the home page is stored.
The predefined variable:
$_SERVER['DOCUMENT_ROOT']
It produces something like this:
/home/yoursite/public_html
Using it in a path:
<?php include($_SERVER['DOCUMENT_ROOT'].”/somefolder/somefile.html”); ?>
Predefined variable for images
This path is relative to the web root (unlike the path for the include file).
The predefined variable:
$_SERVER['HTTP_HOST']
It produces something this:
www.yoursite.com
Using it in a path:
<img src=”<?php echo $_SERVER['HTTP_HOST'];?>/images/logo.png”>
Learn more about predefined variables here: http://php.net/manual/en/reserved.variables.php
Everything’s obvious in hindsight, eh?