PHP: Defining relative paths compatible with HTML Tags

html, parsing, php, relative-path

Solution

Try setting the `<base>` tag in the `<head>` section of your code.

All your images, css, and js files will use this instead of the url in the address bar.

Info on base

Problem

Consider the following directory structure: - ROOT - ------ images - ............... logo.png - ------ includes - ............... vars.php - ------ layout - ............... content.php - ------ index.php How do I define a path constant for logo.png in vars.php that is accessible in both index.php and content.php? Should be compatible with HTML Tags as a relative path. ``` <img src="<?php echo IMAGE_PATH; ?>"> ``` which should be parsed as ``` <img src="images/logo.png"> <!-- if used in index.php --> ``` and ``` <img src="../images/logo.png"> <!-- if used in content.php --> ``` New Question (EDIT): Does root-relative path work when including php files using include / require methods?

Original source