473,657 Members | 2,996 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

variable scope problem

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.variab les.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
2 2895
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.variab les.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
ray
On Oct 8, 4:05*pm, Curtis <dye...@gmail.c omwrote:
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.variab les.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
2456
by: manish | last post by:
I have created a function, it gives more readability compared to the print_r function. As of print_r, it works both for array or single variable. I just want to add in it, the opton to view the variable name for the case of non array variables. Also I want to show the array name. Is there any way that the variable name that is passed to the function can be displayed. function formattedoutput($object) {
134
7857
by: James A. Donald | last post by:
I am contemplating getting into Python, which is used by engineers I admire - google and Bram Cohen, but was horrified to read "no variable or argument declarations are necessary." Surely that means that if I misspell a variable name, my program will mysteriously fail to work with no error message. If you don't declare variables, you can inadvertently re-use an variable used in an enclosing context when you don't intend to, or
166
8596
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
4
14888
by: Gery D. Dorazio | last post by:
Gurus, If a static variable is defined in a class what is the scope of the variable resolved to for it to remain 'static'? For instance, lets say I create a class library assembly that is strongly name which contains the class where the static variable is defined. This library can be referenced by multiple projects. I am fairly sure the static variable does not survive across the application boundry but does it within the application...
6
1472
by: Maarten | last post by:
Here is a nice problem. I have two scripts, one calling the other with via an include. The script that is included contains a variable, a function and a call to that function. The variable needs to be accessible to the function, so I declare it having a global scope. Then I execute the function, which should echo the value of the variable. Contrary to what I expected, the variable isn't there. The code of the 2 programs is: script1.php
1
25664
pbmods
by: pbmods | last post by:
VARIABLE SCOPE IN JAVASCRIPT LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN ) PREREQS: VARIABLES First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause bad breath)? Scope describes the context in which a variable can be used. For example, if a variable's scope is a certain function, then that variable can only be used in that function. If you were to try to access that variable anywhere else in...
2
1390
by: =?Utf-8?B?SmVzcGVyLCBEZW5tYXJr?= | last post by:
Hi, When I try to compile the below code snippet, I get the compile error listed below in this question. In the lines below the loop, the variable j does not excist, however, trying to create it is not allowed. If it doesn't excist, why can't I create it?. I'm not looking for a solution to this problem - that's quite easy - an explanation however would be great, as I've been intrigued by this for a couple of years now.
5
2228
by: somenath | last post by:
Hi All , I have one question regarding scope and lifetime of variable. #include <stdio.h> int main(int argc, char *argv) { int *intp = NULL; char *sptr = NULL;
112
5423
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions that may print some messages. foo(...) { if (!silent)
3
2039
by: SRoubtsov | last post by:
Dear all, Do you know whether ANSI C (or some other dialects) support the following: * a variable name coincides with a type name, * a structure/union field name coincides with a type name in the same file (.c + all relevant .h's)? e.g.
1
8499
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8601
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7314
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5630
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1937
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.