Skip to content

Instantly share code, notes, and snippets.

@simaovergalho
Last active December 16, 2015 18:58
Show Gist options
  • Save simaovergalho/5481271 to your computer and use it in GitHub Desktop.
Save simaovergalho/5481271 to your computer and use it in GitHub Desktop.
PHP: global vars for normalizing the ROOT and HTTP paths of the app.
<?php
// a.php: assuming this included everywhere at very first line
// and located in root directory
// preferable, define a constant instead of variable, cos it
// may used in functions directly without "global $ROOT";
// to use for "include"
define('ROOT', __DIR__); // for PHP >= 5.3
define('ROOT', realpath(dirname(__FILE__))); // for PHP < 5.3
// to use for "src,href"
define('HTTP', 'http://www.mydomain.com/');
// includes
include ROOT .'/layout_header.php';
include ROOT .'/classes/mailer.php';
include ROOT .'/foo/bar/baz.php';
...
// src,href
printf('<img src="%s/img/lollipop.png" />', HTTP);
printf('<script src="%s/js/move.js" />', HTTP);
print '<a href="'. HTTP .'/aFolder/movement.php">Click here!</a>';
...
// inline without global vars includes
define('ROOT', realpath(__DIR__ . '/../'));
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment