|
Posted by Charles O'Flynn on 09/27/06 07:54
"Charles O'Flynn" <charles@matchwalk.com> wrote in message
news:12hka05ljfihp35@corp.supernews.com...
|
| "Oli Filth" <catch@olifilth.co.uk> wrote in message
| news:fEiSg.32264$TF5.8307@newsfe1-win.ntli.net...
|| Jerry Stuckle said the following on 26/09/2006 22:07:
|| > Charles O'Flynn wrote:
|| >> Thanks for the quick reply, Johnny, but I've been looking at the page
| you
|| >> refer to all afternoon and it doesn't seem to work for me. For
| instance,
|| >> (and I'm only illustrating the specific problem I seem to have
|| >> hereunder)...
|| >> ------------------------------------
|| >> $variable;
|| >>
|| >> function printsomething()
|| >> {
|| >> global $variable;
|| >>
|| >> $variable = 'Test'.<b />;
|| >> echo $variable;
|| >> }
|| >>
|| >> printsomething();
|| >> echo $variable;
|| >> ------------------------------------
|| >>
|| >> ...only prints one line of 'Test' - I'd have thought it should print
|| >> out two
|| >> copies. BTW, I'm running under PHP 4.1.2 (and it's not mine to
|| >> change/upgrade!)
|| >
|| > You're close. But you have to use the global keyword in the global
|| > context, also. Not just in the function.
||
|| Umm, no you don't!
||
|
http://uk.php.net/manual/en/language.variables.scope.php#language.variables.scope.global
||
|| >
|| > global $variable;
|| >
|| > function printsomething()
|| > {
|| > global $variable;
|| >
|| > $variable = 'Test'.<b />;
|| > echo $variable;
|| > }
|| >
|| > printsomething();
|| > echo $variable;
|| >
||
||
|| --
|| Oli
|
|
| Thanks, Oli
| I'm getting the feeling, (although noone's spelling it out either here or
in
| any of the myriad books I've looked at for inspiration), that declaring a
| variable as global inside a function will make it accessible outside the
| function at the global scope; in other words, what I've done above is
| declare two independent variables, the one outside the function
over-riding
| the effect of the one inside the function.. OK - I can test this very
| quickly. But if so, how on earth do I get to access it within another
| function, or does this automatically make it visible everywhere?
| Of course, I could store the data within MySQL, thereby making it
| persistent, but this seems like overkill. How does PHP make variables
| accessible with 'real' global scope, not just 'global, except inside
| functions', which for an old 'C' programmer like me, is not global at all?
| I know, in theory about superglobals but again, this seems like overkill.
| Or am I being silly?
| Thanks,
| Charles
|
Problem now solved (from the point of view of this specific query). Thanks
to Oli, Norm and Johnny for taking the trouble to reply.
Regards,
Charles
[Back to original message]
|