/    Sign up×
Community /Pin to ProfileBookmark

I am perplexed why this global is not working

I have placed the following code in a PHP script and the global, $tt, does not have a value within the “test()” function. Am I missing something so simple I cannot see it?

[code]
$tt = 10;
print_r(“tt: ” . $tt . “<br>”);
// Test
function test() {
global $tt;
print_r(“tt inside: ” . $tt . “<br>”);
}
print_r(“I am here<br>”);
test(); exit;
[/code]

Many thanks…

Todd

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMar 05.2015 — Works okay for me (after I stick a "<?php" tag at the start).

What sort of output do you get? I got this, running it from the command line:
<i>
</i>$ php global.php
tt: 10&lt;br&gt;I am here&lt;br&gt;tt inside: 10&lt;br&gt;
Copy linkTweet thisAlerts:
@NogDogMar 05.2015 — PS: Mind you, I'm not a fan of [i]ever[/i] using global. ?
Copy linkTweet thisAlerts:
@Stephen_PhilbinMar 05.2015 — I tend not to use the global keyword either, but think it <em>might</em> be because when you declare the $tt to be global in test() you're masking the already global $tt that's declared at the start of the script. Try just chucking out the global keyword from the test() function and see if it suddenly starts behaving.
Copy linkTweet thisAlerts:
@NogDogMar 06.2015 — I tend not to use the global keyword either, but think it <em>might</em> be because when you declare the $tt to be global in test() you're masking the already global $tt that's declared at the start of the script. Try just chucking out the global keyword from the test() function and see if it suddenly starts behaving.[/QUOTE]

That's the purpose of "global", to allow access to a variable that was created in the global scope inside of the function's scope. The cited script should have worked as it did for me, accessing the global value of $tt within the function.

However, global injects a strong dependency between the function and any code that uses it. Therefore, its almost always (maybe strike that word "almost") to pass variables into your functions as function parameters instead of using global -- and pass them in by reference if you want/need the function to operate directly on that variable.
Copy linkTweet thisAlerts:
@Stephen_PhilbinMar 06.2015 — That's the purpose of "global", to allow access to a variable that was created in the global scope inside of the function's scope.[/QUOTE]

Perhaps I was thinking of variable scoping in Javascript, then. Probably getting it muddled up in my head with the difference vetween declaring variables with the [i]var[/i] keyword and not using it. In any case I'll just but out 'cause it's clearly got nothing to do with the original question.
Copy linkTweet thisAlerts:
@NogDogMar 06.2015 — It's the reverse for me: I always have to fumble around and remind myself when to use "var" in JS and why. ? In PHP it's easy: just avoid using "global" if at all possible (and really it's always possible), and you're gold. ?
Copy linkTweet thisAlerts:
@Stephen_PhilbinMar 07.2015 — Oh I'm just a moron and forget almost everything. It's just the odd bits that I remember (and it turns out I don't even remeber for what language). I [b]very[/b] rarely use Javascript at all. That's probably why that bit of information stuck. Because I had to look it up when I recently wrote some Javascript for the fist time in ages. I guess it must show how little I use variables in functions without passing them in as arguments too, if I've been happily coding away, completely oblivious to such an important distinction.
Copy linkTweet thisAlerts:
@rootMar 08.2015 — I don't see anything wrong with the function, in all likelyhood it would either be what NogDog said, missing <?php tag at the beginning or the browsers cache is interfering with the result of the script. What browser(s) was this happening in?
×

Success!

Help @rtcary 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.20,
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,
)...