4WebHelp
 FAQ  •  Search  •  User Groups  •  Forum Admins  •  Smilies List  •  Statistics  •  Rules   •  Login   •  Register
Toggle Navigation Menu

 Globals
Post New TopicReply to Topic
View Previous Topic Print this topic View Next Topic
Author Message
Iyonix
WebHelper
WebHelper


Joined: 12 Nov 2002
Posts: 82
Location: Yarm, England

PostPosted: Wed Jan 22, 2003 7:55 pm (14 years, 9 months ago) Reply with QuoteBack to Top

Can some one explain to me when I should use global and when I shouldn't? I have been wring a PHP script with functions and having trouble with variables be transfered between the functions and the main script.

Thanks,

________________________________
Iyonix
OfflineView User's ProfileFind all posts by IyonixSend Personal Message
Daniel
Team Member



Joined: 06 Jan 2002
Posts: 2564

PostPosted: Wed Jan 22, 2003 8:03 pm (14 years, 9 months ago) Reply with QuoteBack to Top

In a function, no variables are accessible, except those passed along to the function, like this:

function($variable1, $variable2);

So, if you need to access, for example, $PHP_SELF, in your function, you'll need to declare it as global, like so:

Code:
global $PHP_SELF;


You can also do away with this and call $GLOBALS['PHP_SELF'] instead.

________________________________
Image
OfflineView User's ProfileFind all posts by DanielSend Personal Message
Iyonix
WebHelper
WebHelper


Joined: 12 Nov 2002
Posts: 82
Location: Yarm, England

PostPosted: Wed Jan 22, 2003 9:30 pm (14 years, 9 months ago) Reply with QuoteBack to Top

Thanks Smile

If I set a variable in a function

Code:

function decreaseturns() {
  blah blah blah... lots of code
  $turns=value;
  blah blah blah... lots of code
  }


how would I make it so could access the variable $turns from the rest of the script? (not in the function)

Thanks,

________________________________
Iyonix
OfflineView User's ProfileFind all posts by IyonixSend Personal Message
Iyonix
WebHelper
WebHelper


Joined: 12 Nov 2002
Posts: 82
Location: Yarm, England

PostPosted: Wed Jan 22, 2003 9:31 pm (14 years, 9 months ago) Reply with QuoteBack to Top

Daniel wrote:


Code:
global $PHP_SELF;




Also... Would I write this in the function or in the main script?

Thanks,

________________________________
Iyonix
OfflineView User's ProfileFind all posts by IyonixSend Personal Message
Daniel
Team Member



Joined: 06 Jan 2002
Posts: 2564

PostPosted: Thu Jan 23, 2003 6:59 am (14 years, 9 months ago) Reply with QuoteBack to Top

I'm not sure. I think you don't need to do anything. But I don't understand why you'd want to do that Question

In the function, like this:

Code:
function blabla($variable1, $variable2) {
global $PHP_SELF;
}

________________________________
Image
OfflineView User's ProfileFind all posts by DanielSend Personal Message
Darren
Team Member



Joined: 05 Feb 2002
Posts: 549
Location: London

PostPosted: Thu Jan 23, 2003 9:36 am (14 years, 9 months ago) Reply with QuoteBack to Top

Iyonix wrote:
Thanks Smile

If I set a variable in a function

Code:

function decreaseturns() {
  blah blah blah... lots of code
  $turns=value;
  blah blah blah... lots of code
}


how would I make it so could access the variable $turns from the rest of the script? (not in the function)

Thanks,


It would be like this, wouldn't it?

Code:

function decreaseturns() {
  blah blah blah... lots of code
  $turns=value;
  blah blah blah... lots of code
  return $turns;
  }

$turns = decreaseturns();
OfflineView User's ProfileFind all posts by DarrenSend Personal MessageVisit Poster's Website
Daniel
Team Member



Joined: 06 Jan 2002
Posts: 2564

PostPosted: Thu Jan 23, 2003 4:46 pm (14 years, 9 months ago) Reply with QuoteBack to Top

Well it really depends on what you're looking to do... You might want to return another variable. Anyway, we'll soon find out what Iyonix wants to do Very Happy.

________________________________
Image
OfflineView User's ProfileFind all posts by DanielSend Personal Message
Hertzsprung
WebHelper
WebHelper


Joined: 30 Jul 2002
Posts: 56
Location: UK

PostPosted: Thu Jan 23, 2003 6:30 pm (14 years, 9 months ago) Reply with QuoteBack to Top

Daniel wrote:


You can also do away with this and call $GLOBALS['PHP_SELF'] instead.


I believe that this is a far more elegant way of doing things, especially with other super-globals such as $SESSION, $_GET and $_POST.

Iyonix wrote:
Thanks Smile

how would I make it so could access the variable $turns from the rest of the script? (not in the function)



This is generally bad practise -- perhaps there is a better way to achieve this?
OfflineView User's ProfileFind all posts by HertzsprungSend Personal MessageVisit Poster's Website
Display posts from previous:      
Post New TopicReply to Topic
View Previous Topic Print this topic View Next Topic


 Jump to:   




You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot edit your posts in this forum.
You cannot delete your posts in this forum.
You cannot vote in polls in this forum.


Page generation time: 0.055622 seconds :: 17 queries executed :: All Times are GMT
Powered by phpBB 2.0 © 2001, 2002 phpBB Group :: Based on an FI Theme