/    Sign up×
Community /Pin to ProfileBookmark

Where To Put Globals

Hi All,
Just a quick question about structure.
I had a script that looked comething like this…

[code=php]//declare globals
global $test, $g1, $g2, $path

//use the globals
$g1 = “something”

//include a fragment
include(setup.inc.php”);//uses the global $test[/code]

I’ve changed it to this…

[code=php]//get global declaration
include(“globals.inc.php”);

//use the globals
$g1 = “something”

//include a fragment
include(setup.inc.php”);//uses the global $test[/code]

In other words, I’ve moved the global declaration into a scriptlet that I’m including. It seems to work, but because all I’m doing is setting globals, it will appear to work anyway, except that the end result might be unstable.

Therefore, the question is…
Does it matter where I declare globals?

Thanks
CTB

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@SlappyTheFishNov 01.2007 — Not sure I entirely understand the problem, but all variables created in the global namespace (i.e. not inside a class or a function) are declared as global anyway.

It shouldn't matter where you declare them as long as you keep track of them and don't accidentally overwrite them or anything.
Copy linkTweet thisAlerts:
@chestertbauthorNov 01.2007 — Thanks Slappy.

As I understand it (and maybe I'm wrong... it wouldn't be the first time), if you want to use a variable in a fragment that's loaded via an include(), it needs to be global.

Perhaps I'm trying to anticipate a problem that doesn't exist.

Cheers

CTB
Copy linkTweet thisAlerts:
@pcthugNov 02.2007 — Any variables used/declared do not need to be global if they are in a script that is included (unless of course you are using them outside of the global scope in functions of classes). Imagine any included php code just replace the expression that included it;

[B]Foo.php[/B][code=php]<?php

$a = 'Hello';

include 'b.php';

echo $a; // World[/code]


[B]Bar.php[/B][code=php]<?php

$a = 'World';[/code]


In the end it is just one big script;

Foobar.php
[code=php]<?php

$a = 'Hello';

// Bar.php - naturally opening/closing tags are omitted by the parser
$a = 'World';


echo $a; // World[/code]
Copy linkTweet thisAlerts:
@NogDogNov 02.2007 — As others have alluded to, declaring a variable as global only has meaning within a user-defined function definition. If you want to make a given variable automatically global in scope so that it is available to any function in the script (or its includes), you can either define() it as a constant if its value should, in fact, be constant; or you can use the $GLOBALS array, which is a "super-global" array just like $_POST or $_SERVER.
[code=php]
<?php
define('MY_CONSTANT', 'Hello');
$GLOBALS['my_global'] = 'World';
function example()
{
echo MY_CONSTANT . ', ' . $GLOBALS['my_global'] . '!';
}
example();
[/code]
Copy linkTweet thisAlerts:
@chestertbauthorNov 02.2007 — Yeah. That's what I thought... the bit about an included script just replacing the statement that included it... right up until the time when it didn't work.

What I discovered was that if I want to use a variable to pass a value to or from an included script snippet, I needed to declare the variable as a global in much the same way you'd do it for a function. I don't know why... I just know that I didn't get the predicted result until I declared the variable as global.

I even came here and asked the question (http://www.webdeveloper.com/forum/showthread.php?t=56505) and found that it was more efficient to declare the globals in the calling script rather than the include3d script as suggested.

Ah well... maybe it's just that the mice inside my desktop box weren't feeling well that day.

Thanks all. Help, as always, has been much appreciated.
Copy linkTweet thisAlerts:
@bokehNov 02.2007 — The main problem with globals and included files is namespace collision, which is when the globals inside the included file overwrite the globals in the including file.
×

Success!

Help @chestertb spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...