473,509 Members | 2,671 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Variable Scope in Include Files

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
6 3376
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
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
.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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2308
by: Deane Barker | last post by:
I have a function that selects a file to include, then includes is. The file is including within the function, like so: function include_file($file_name) { require $file_name; return; }
5
27965
by: William | last post by:
In Peer.h, I have: class Peer { // ... }; In Overseer.h, I have: #include "Peer.h" #include <vector>
3
7634
by: Mike | last post by:
I'm new to PHP - moving over from ASP. I have a number of include files, the first of which sets the value of a variable $loginmsg. I use that variable in a subsequent include file, but get a...
3
3917
by: Datta Patil | last post by:
Hi , #include<stdio.h> func(static int k) /* point2 : why this is not giving error */ { int i = 10 ; // static int j = &i ; /* point 1: this will give compile time error */ return k; } /*...
4
14867
by: Gery D. Dorazio | last post by:
Gurus, If a static variable is defined in a class what is the scope of the variable resolved to for it to remain 'static'? For instance, lets say I create a class library assembly that is...
2
1683
by: Bit byte | last post by:
What's wrong with this: //file1.h Class A { public: A(){i=1;} ~A(){;} void doThis(void){;} void doThat(void){;}
3
5429
by: pelleas | last post by:
Hello to all! 1a. I'm a newbie having trouble accessing a pointer from one .cpp file which is defined within a member function found in a separate .cpp file. I'm looking for a line or two of...
1
25621
pbmods
by: pbmods | last post by:
VARIABLE SCOPE IN JAVASCRIPT LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN ) PREREQS: VARIABLES First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause...
11
2899
by: whirlwindkevin | last post by:
I saw a program source code in which a variable is defined in a header file and that header file is included in 2 different C files.When i compile and link the files no error is being thrown.How is...
0
7135
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7342
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7067
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7505
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5650
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5060
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3215
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1570
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
440
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.