NYCPHP Meetup

NYPHP.org

[nycphp-talk] Setting Constants Globally?

Hans Zaunere hans at nyphp.org
Fri May 9 13:57:28 EDT 2003


--- Jim Musil <jmusil at villagevoice.com> wrote:
> 
> Hi,
> 
> Pardon me if the answer to this question is obvious, but is there a way to
> define my own constants that are global for all php scripts?
> 
> I see that I could auto_prepend a file to every script, but I'm more
> interested in being able to set my own constant.
> 
> Something like PATH_TO_DEVTOOLS ...
> 
> Maybe there is a better way to do something like this?

There are several options.

If you need constants, then the only way is to:

define('SOMETHING','SOMEVALUE');

in an include-type of file.  I'm a big fan of auto_prepend.

If you don't need it to be a constant (heck, if someone changes the value,
it's their lose) and you're using mod_php, I very much like something like
this in httpd.conf:

SetEnv SOMETHING 'SOMEVALUE'

which will be accesible via $_SERVER['SOMETHING'];  This is very handy and I
use it alot.  Also note that you can SetEnv in .htaccess, <Directory> or even
<Files> directives, so it's a real flexible way of setting things up.

Lastly, you can use shared memory.  Once set, they are available until reboot
(or they are released).  This is also extremely handy, and the most flexible,
but is a bit more involved to get started with.

And, I seem to remember there was a way to actually make a constant
definition in Apache's httpd.conf but I can't recall it now.  Oh well, maybe
I'll think of it.

H




More information about the talk mailing list