Bluehost.com Web Hosting $6.95

Global variables

This is a discussion on Global variables within the PHP Language forums, part of the PHP Programming Forums category; "Charles O'Flynn" <charles@matchwalk.com> wrote in message news:12hkbhi2nnhpo27@corp.supernews.com... > "...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #11 (permalink)  
Old 09-27-2006
Norman Peelman
 
Posts: n/a
Default Re: Global variables

"Charles O'Flynn" <charles@matchwalk.com> wrote in message
news:12hkbhi2nnhpo27@corp.supernews.com...
> "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...s.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
>


Of course, there's always the $_GLOBALS array...

Norm


Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 12:30 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0