Connecting Tech Pros Worldwide Help | Site Map

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

NotGiven
Guest
 
Posts: n/a
#1: Jul 17 '05
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;
}


Andy Hassall
Guest
 
Posts: n/a
#2: Jul 17 '05

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


On Fri, 27 Aug 2004 17:26:10 -0400, "NotGiven" <noname@nonegiven.net> wrote:
[color=blue]
>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;
>}[/color]

Look up the 'global' statement.

--
Andy Hassall / <andy@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Guest
 
Posts: n/a
#3: Jul 17 '05

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


> In this file I have variables I'd like to use in the calling page and[color=blue]
> functions called by that page.[/color]

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


steve
Guest
 
Posts: n/a
#4: Jul 17 '05

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


"laidbak69" wrote:[color=blue][color=green]
> > In this file I have variables I’d like to use in the calling[/color]
> page and[color=green]
> > functions called by that page.[/color]
>
> 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[/color]
defined[color=blue]
> 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[/color]
variable[color=blue]
> you
> should use the $GLOBALS array.
>
> ____________________________________
> Wil Moore III, MCP | Integrations Specialist[/color]

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
NotGiven
Guest
 
Posts: n/a
#5: Jul 17 '05

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


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!






<laidbak69@hotmail.com> wrote in message
news:10ivhjsoh0ja44c@corp.supernews.com...[color=blue][color=green]
> > In this file I have variables I'd like to use in the calling page and
> > functions called by that page.[/color]
>
> You can use the a variable globally by using the superglobal array called
> $GLOBALS instead of the keyword global.
> Check here for more information:[/color]
http://us4.php.net/language.variables.scope[color=blue]
>
>
>
> There is another way, and this method is one I prefer personally.
> You can use the define keyword to define a constant. Constants defined[/color]
this[color=blue]
> 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
>
>[/color]


Andy Hassall
Guest
 
Posts: n/a
#6: Jul 17 '05

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


On Sat, 28 Aug 2004 07:32:24 -0400, "NotGiven" <noname@nonegiven.net> wrote:
[color=blue]
>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);[/color]

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.
[color=blue]
>echo mName;
>//prints "mName" - should print "My name"[/color]

Again, 'use of undefined constant mName, assumed string'.
[color=blue]
>What do I change - Many many thanks![/color]

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

--
Andy Hassall / <andy@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Closed Thread