Variable Scope in Include Files

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I'm tying myself in knots trying to figure out variable scope with
constants and include files.  This is what I'm doing:

A page (index.php) on my website includes a general purpose include
file ( that declares a constant DAY_NUM.  A little later in the
file, includes another file ( that tries to use
DAY_NUM.  I'm getting a "Use of Undefined Constant" notice as a result
of this line when I load index.php in the browser.

The index.php also refers to DAY_NUM after the include(
statement without a problem.  When I move the DAY_NUM constant
declaration statement to, I get rid of the first "Undefined
Constant" notice, but then I get a new one where index.php refers to

(I hope that makes sense.)

I was assuming that any constant had global scope and therefore when I
declared it in my file, any subsequent script or files included
would have access to it.  But that appears not to be the case.  Is this
counter-intuitive, or am I missing something here?

I found this note on the PHP site
( ), but it's a
bit vaguely worded and doesn't refer specifically to constants:

"if you define a function in that included file, it can't get the
variables from the inluded file's scope. global won't work. The only
way to give such an include function access to global vars is via
arguments. I don't know if this is a bug in PHP."

And I'm getting the notice even when I refer to the constant outside a
function in the include file.

If it is the case that constants declared in an included file don't
have global scope, please let me know, as I'll have to rethink my site



Re: Variable Scope in Include Files

Tom wrote:
Quoted text here. Click to load it

Where and how are you defining DAY_NUM?

If you put its definition inside a function, the constant will only be
defined *after* the function is called.

define('GOOD_CONSTANT', 'ok');
function dummy() {
  define('BAD_CONSTANT', 'oops');



Mail to my "From:" address is readable by all at /
== ** ## !! ------------------------------------------------ !! ## ** ==
may bypass my spam filter. If it does, I may reply from another address!

Re: Variable Scope in Include Files

Thanks for the response.  The answer came to me in the shower today.  I
was converting all my file paths to absolute paths.  Since I'm on a
Windows machine, I wanted to get around the backslash/frontslash
problem.  But this runs afoul of another rule mentioned on the PHP

"Please don't forget:
values of included (or required) file variables are NOT available in
the local script if the included file resides on a remote server:"

Though it is a local script, I guess using the absolute
(http://localhost/) path rather than the $_SERVER['document_root']
constant in my file paths makes it look like a remote server to the

Now if I can just find that slash converter function I saw the other


Re: Variable Scope in Include Files


Quoted text here. Click to load it

Yep. The parser does what you told him to do and requests the file via
HTTP, even if it's on the same machine.

Quoted text here. Click to load it

The predefined constant DIRECTORY_SEPARATOR might be of interest.


Re: Variable Scope in Include Files

Quoted text here. Click to load it

Never ever pass URLs to include or require. It's stupid that the manual even
mentions the variable scope issue (actual behavior depends on server
configruation). It should simply tell you to always use filesystem paths.

As for the back-slash/forward-slash problem, just use forward slashes all
the time. PHP is smart enough to convert them to back-slashes.

Re: Variable Scope in Include Files

Quoted text here. Click to load it

And there is the PATH_SEPARATOR constant..

Met vriendelijke groeten,
Tim Van Wassenhove <

Re: Variable Scope in Include Files

Thanks for the replies.  The path problem turned out to be a simple
syntax error -- I was missing a slash in my path.


Site Timeline