如何定义“系统范围”常量,该常量是包含特定文件夹目录的字符串?

I store all of my scripts in the same place on the server and reference then using PHP include statements. But what if I want or need to change that directory? Do I really want to change every statement? Absolutely not.

One thing that I debated doing was to include a file named 'scriptsdir.php' that would be in the home directory for easy access. All this file would be responsible of doing is to define the constant 'SCRIPTS_DIR' as that directory. This suffices but if THAT file has to be moved or something happens to it, etc. I was wondering if I could define a constant that would be accessible without ever having to manually define it.

Is there a way to do this in php.ini or .htaccess or anything?

Another option is to declare a variable $_SERVER["SCRIPTS_DIR"] via .htaccess using:

 SetEnv SCRIPTS_DIR /var/www/project123/includes/

That would be available reliably to all scripts beneath the document_root and can often simplify relocations.

You could do something like this with a magic constant...

define('DOCROOT', dirname(__FILE__));

If using >= PHP 5.3 you can simply use __DIR__.

Then you would include your files using...

include DOCROOT . '/bootstrap.php';

If you require it to be system-wide across all PHP applications then you can use auto_prepend_file with a file you define it in.