Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old October 8th, 2008, 07:25 AM
ray
Guest
 
Posts: n/a
Default variable scope problem

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.
  #2  
Old October 8th, 2008, 09:15 AM
Curtis
Guest
 
Posts: n/a
Default Re: variable scope problem

ray wrote:
Quote:
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
  #3  
Old October 9th, 2008, 04:15 AM
ray
Guest
 
Posts: n/a
Default Re: variable scope problem

On Oct 8, 4:05*pm, Curtis <dye...@gmail.comwrote:
Quote:
ray wrote:
Quote:
Hi, all,
foreach($array as $k =$v) {
* * *$foo = ...;
}
echo $foo;
>
Quote:
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.
 

Bookmarks

Thread Tools

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

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles