469,645 Members | 1,185 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,645 developers. It's quick & easy.

can you declare global scope in an included file and see those variables throughout other code?

I have an file I call using:
require_once()

In this file I have variables I'd like to use in the calling page and
functions called by that page.

How can I do this?

example:

<page1.php>
require_once(code1.php);
require_once(utilities.php);

Function simple() {
$varA = $code1_var_A + $code1_var_B;
return varA;
}
Jul 17 '05 #1
5 2662
On Fri, 27 Aug 2004 17:26:10 -0400, "NotGiven" <no****@nonegiven.net> wrote:
I have an file I call using:
require_once()

In this file I have variables I'd like to use in the calling page and
functions called by that page.

How can I do this?

example:

<page1.php>
require_once(code1.php);
require_once(utilities.php);

Function simple() {
$varA = $code1_var_A + $code1_var_B;
return varA;
}


Look up the 'global' statement.

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #2
> In this file I have variables I'd like to use in the calling page and
functions called by that page.


You can use the a variable globally by using the superglobal array called
$GLOBALS instead of the keyword global.
Check here for more information: http://us4.php.net/language.variables.scope

There is another way, and this method is one I prefer personally.
You can use the define keyword to define a constant. Constants defined this
way are automatially global, so you can access them from included scripts
and functions as well.

define('MYVARIABLE', 'stringvalue');

print MYVARIABLE;

http://us4.php.net/define

If you know you will not need to change the value you should use
constants... if you have a reason to modify the value of the variable you
should use the $GLOBALS array.

____________________________________
Wil Moore III, MCP | Integrations Specialist
Jul 17 '05 #3
"laidbak69" wrote:
In this file I have variables I’d like to use in the calling page and
functions called by that page.


You can use the a variable globally by using the superglobal array
called
$GLOBALS instead of the keyword global.
Check here for more information:
http://us4.php.net/language.variables.scope

There is another way, and this method is one I prefer personally.
You can use the define keyword to define a constant. Constants

defined this
way are automatially global, so you can access them from included
scripts
and functions as well.

define(’MYVARIABLE’, ’stringvalue’);

print MYVARIABLE;

http://us4.php.net/define

If you know you will not need to change the value you should use
constants... if you have a reason to modify the value of the variable you
should use the $GLOBALS array.

____________________________________
Wil Moore III, MCP | Integrations Specialist


I have had problems getting $GLOBALS to work reliably for me when
defined deep. I therefore prefer to use constants when declaring a
variable deep in a function, or in an include.

--
http://www.dbForumz.com/ This article was posted by author's request
Articles individually checked for conformance to usenet standards
Topic URL: http://www.dbForumz.com/PHP-declare-...ict144180.html
Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbForumz.com/eform.php?p=482423
Jul 17 '05 #4
what is happening now is this. I definemy constants in code1.php but I can
not reference them in my main code.

<code1.php>
//define my constant
define("mName", "My name");
<end of page>

<page1.php>
require_once(code1.php);
echo mName;
//prints "mName" - should print "My name"
What do I change - Many many thanks!


<la*******@hotmail.com> wrote in message
news:10*************@corp.supernews.com...
In this file I have variables I'd like to use in the calling page and
functions called by that page.
You can use the a variable globally by using the superglobal array called
$GLOBALS instead of the keyword global.
Check here for more information:

http://us4.php.net/language.variables.scope
There is another way, and this method is one I prefer personally.
You can use the define keyword to define a constant. Constants defined this way are automatially global, so you can access them from included scripts
and functions as well.

define('MYVARIABLE', 'stringvalue');

print MYVARIABLE;

http://us4.php.net/define

If you know you will not need to change the value you should use
constants... if you have a reason to modify the value of the variable you
should use the $GLOBALS array.

____________________________________
Wil Moore III, MCP | Integrations Specialist

Jul 17 '05 #5
On Sat, 28 Aug 2004 07:32:24 -0400, "NotGiven" <no****@nonegiven.net> wrote:
what is happening now is this. I definemy constants in code1.php but I can
not reference them in my main code.

<code1.php>
//define my constant
define("mName", "My name");
<end of page>

<page1.php>
require_once(code1.php);
This isn't right for starters, it'll try and include a file 'code1php' and
should emit two warnings.

(use of undefined constant code1, assumed string, same again for php, then
it'll concatenate the two with the '.' operator between then).

Put the filename in quotes.
echo mName;
//prints "mName" - should print "My name"
Again, 'use of undefined constant mName, assumed string'.
What do I change - Many many thanks!


Set error_reporting to E_ALL and make sure display_errors is on - you're
coding blindfolded at the moment!

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Tony Johansson | last post: by
6 posts views Thread by rick | last post: by
11 posts views Thread by Capstar | last post: by
7 posts views Thread by Michael | last post: by
41 posts views Thread by Miguel Dias Moura | last post: by
44 posts views Thread by fabio | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.