By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,349 Members | 1,353 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,349 IT Pros & Developers. It's quick & easy.

variable scope problem

P: n/a
ray
Hi, all,
foreach($array as $k =$v) {
$foo = ...;
}
echo $foo;

Is it allowed to access the $foo variable that is created within the
loop from outside of the loop? I think it isn't allowed, because
according to the rule stated in PHP manual(http://www.php.net/manual/
en/language.variables.scope.php): The scope of a variable is the
context within which it is defined, the $foo variable is defined
inside the loop, so its scope should limit within that loop. But the
test result of such script shows that I can access the $foo variable
from outside of the loop correctly. WHY? I'm a PHP newbie, Could
somebody help me out? Thanks in advance.
Oct 8 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
ray wrote:
Hi, all,
foreach($array as $k =$v) {
$foo = ...;
}
echo $foo;

Is it allowed to access the $foo variable that is created within the
loop from outside of the loop? I think it isn't allowed, because
according to the rule stated in PHP manual(http://www.php.net/manual/
en/language.variables.scope.php): The scope of a variable is the
context within which it is defined, the $foo variable is defined
inside the loop, so its scope should limit within that loop. But the
test result of such script shows that I can access the $foo variable
from outside of the loop correctly. WHY? I'm a PHP newbie, Could
somebody help me out? Thanks in advance.
There is no lexical scoping in PHP. Did you read any further than the
first sentence of the page? From your example, it's not clear in which
context $foo was defined (global or local scope), although you can't
localize vars to just any block. Vars in a function or class
definition are limited to a local scope, unless you explicitly use the
"global" keyword or $GLOBALS array.

--
Curtis
Oct 8 '08 #2

P: n/a
ray
On Oct 8, 4:05*pm, Curtis <dye...@gmail.comwrote:
ray wrote:
Hi, all,
foreach($array as $k =$v) {
* * *$foo = ...;
}
echo $foo;
Is it allowed to access the $foo variable that is created within the
loop from outside of the loop? I think it isn't allowed, because
according to the rule stated in PHP manual(http://www.php.net/manual/
en/language.variables.scope.php): The scope of a variable is the
context within which it is defined, the $foo variable is defined
inside the loop, so its scope should limit within that loop. But the
test result of such script shows that I can access the $foo variable
from outside of the loop correctly. WHY? I'm a PHP newbie, Could
somebody help me out? Thanks in advance.

There is no lexical scoping in PHP. Did you read any further than the
first sentence of the page? From your example, it's not clear in which
context $foo was defined (global or local scope), although you can't
localize vars to just any block. Vars in a function or class
definition are limited to a local scope, unless you explicitly use the
"global" keyword or $GLOBALS array.

--
Curtis
Thanks for your reply.
What means lexical scoping? Do you mean that only a function can
establish a local scope?
I did read the whole page of the manual page and it does not
explicitly clarify this issue.
Oct 9 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.