Author |
Message |
Iyonix
WebHelper
Joined: 12 Nov 2002
Posts: 82
Location: Yarm, England
|
Posted:
Wed Jan 22, 2003 7:55 pm (21 years, 3 months ago) |
|
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 |
|
|
|
Daniel
Team Member
Joined: 06 Jan 2002
Posts: 2564
|
Posted:
Wed Jan 22, 2003 8:03 pm (21 years, 3 months ago) |
|
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:
You can also do away with this and call $GLOBALS['PHP_SELF'] instead. |
________________________________
|
|
|
|
Iyonix
WebHelper
Joined: 12 Nov 2002
Posts: 82
Location: Yarm, England
|
Posted:
Wed Jan 22, 2003 9:30 pm (21 years, 3 months ago) |
|
Thanks
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 |
|
|
|
Iyonix
WebHelper
Joined: 12 Nov 2002
Posts: 82
Location: Yarm, England
|
Posted:
Wed Jan 22, 2003 9:31 pm (21 years, 3 months ago) |
|
Also... Would I write this in the function or in the main script?
Thanks, |
________________________________ Iyonix |
|
|
|
Daniel
Team Member
Joined: 06 Jan 2002
Posts: 2564
|
Posted:
Thu Jan 23, 2003 6:59 am (21 years, 3 months ago) |
|
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
In the function, like this:
Code: | function blabla($variable1, $variable2) {
global $PHP_SELF;
} |
|
________________________________
|
|
|
|
Darren
Team Member
Joined: 05 Feb 2002
Posts: 549
Location: London
|
Posted:
Thu Jan 23, 2003 9:36 am (21 years, 3 months ago) |
|
Iyonix wrote: | Thanks
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();
|
|
|
|
|
|
Daniel
Team Member
Joined: 06 Jan 2002
Posts: 2564
|
Posted:
Thu Jan 23, 2003 4:46 pm (21 years, 3 months ago) |
|
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 . |
________________________________
|
|
|
|
Hertzsprung
WebHelper
Joined: 30 Jul 2002
Posts: 56
Location: UK
|
Posted:
Thu Jan 23, 2003 6:30 pm (21 years, 3 months ago) |
|
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
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? |
|
|
|
|
|
Page generation time: 0.15387 seconds :: 18 queries executed :: All Times are GMT
Powered by
phpBB 2.0
© 2001, 2002 phpBB Group :: Based on an FI Theme