By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,587 Members | 1,071 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,587 IT Pros & Developers. It's quick & easy.

Variable Scope in Include Files

P: n/a
Tom
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 (ini.inc) that declares a constant DAY_NUM. A little later in the
file, ini.inc includes another file (b_data.inc) 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(ini.inc)
statement without a problem. When I move the DAY_NUM constant
declaration statement to b_data.inc, I get rid of the first "Undefined
Constant" notice, but then I get a new one where index.php refers to
DAY_NUM.

(I hope that makes sense.)

I was assuming that any constant had global scope and therefore when I
declared it in my ini.inc 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
(http://us2.php.net/manual/en/languag...les.scope.php), 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
architecture.

Thanks,

Tom

Jul 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Tom wrote:
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 (ini.inc) that declares a constant DAY_NUM. A little later in the
file, ini.inc includes another file (b_data.inc) 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.

<snip>

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.
<?php
define('GOOD_CONSTANT', 'ok');
function dummy() {
define('BAD_CONSTANT', 'oops');
}

echo GOOD_CONSTANT, ' and ', BAD_CONSTANT;

dummy();
echo GOOD_CONSTANT, ' and ', BAD_CONSTANT;
?>

--
Mail to my "From:" address is readable by all at http://www.dodgeit.com/
== ** ## !! ------------------------------------------------ !! ## ** ==
TEXT-ONLY mail to the whole "Reply-To:" address ("My Name" <my@address>)
may bypass my spam filter. If it does, I may reply from another address!
Jul 17 '05 #2

P: n/a
Tom
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
page:

"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
parser.

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

Tom

Jul 17 '05 #3

P: n/a
.oO(Tom)
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
parser.
Yep. The parser does what you told him to do and requests the file via
HTTP, even if it's on the same machine.
Now if I can just find that slash converter function I saw the other
day.


The predefined constant DIRECTORY_SEPARATOR might be of interest.

Micha
Jul 17 '05 #4

P: n/a
"Tom" <kl******@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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
page:

"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
parser.

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

Tom


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.
Jul 17 '05 #5

P: n/a
On 2004-12-31, Chung Leong <ch***********@hotmail.com> wrote:
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.

And there is the PATH_SEPARATOR constant..
--
Met vriendelijke groeten,
Tim Van Wassenhove <http://www.timvw.info>
Jul 17 '05 #6

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

Cheers.

Jul 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.