473,513 Members | 2,661 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Variable scope issue

I have a function foo () in a file called funcfoo.php called from my main
program file main.php, which has an include for that file.

Foo () contains a line:

echo $bar;

Now I have a (global) variable $bar which is set in main:

GLOBAL $bar;
$bar = 1;

When I call foo () from main it doesn't echo the value 1. (How) can I make
$bar visible to foo () without explicitly passing it as a parameter ? Is the
only way putting the entire foo function body in the same file as main ?

TIA
Pjotr

Jul 17 '05 #1
3 1888
Pjotr Wedersteers wrote:
I have a function foo () in a file called funcfoo.php called from my main
program file main.php, which has an include for that file.

Foo () contains a line:

echo $bar;

Now I have a (global) variable $bar which is set in main:

GLOBAL $bar;
$bar = 1;


It has no effect to use the "global" modifier in the global scope.

Variable scope and the use of global is very well explained here:
http://www.php.net/manual/en/languag...bles.scope.php

/Bent
Jul 17 '05 #2

"Pjotr Wedersteers" <pj***@wedersteers.com> wrote in message
news:41***********************@news.xs4all.nl...
I have a function foo () in a file called funcfoo.php called from my main
program file main.php, which has an include for that file.

Foo () contains a line:

echo $bar;

Now I have a (global) variable $bar which is set in main:

GLOBAL $bar;
$bar = 1;

When I call foo () from main it doesn't echo the value 1. (How) can I make
$bar visible to foo () without explicitly passing it as a parameter ? Is
the only way putting the entire foo function body in the same file as main
?

TIA
Pjotr


Try using GLOBAL $bar; in function foo() before the echo. This is all
explained in http://www.php.net/manual/en/languag...bles.scope.php

--
Tony Marston

http://www.tonymarston.net

Jul 17 '05 #3
Tony Marston wrote:
"Pjotr Wedersteers" <pj***@wedersteers.com> wrote in message
news:41***********************@news.xs4all.nl...
I have a function foo () in a file called funcfoo.php called from my
main program file main.php, which has an include for that file.

Foo () contains a line:

echo $bar;

Now I have a (global) variable $bar which is set in main:

GLOBAL $bar;
$bar = 1;

When I call foo () from main it doesn't echo the value 1. (How) can
I make $bar visible to foo () without explicitly passing it as a
parameter ? Is the only way putting the entire foo function body in
the same file as main ?

TIA
Pjotr


Try using GLOBAL $bar; in function foo() before the echo. This is all
explained in http://www.php.net/manual/en/languag...bles.scope.php


Thanks, that fixed it!
Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2861
by: Thomas Matthews | last post by:
Hi, While coding programs, I cam about a conundrum regarding variables defined in an iterative loop. The issue is whether it is more efficient to factor the definition out of the loop or...
2
8803
by: Thomas Matthews | last post by:
Hi, I'm getting linking errors when I declare a variable in the global scope, but not inside a function. The declarations are the same (only the names have been changed...). class Book {...
7
2176
by: Michael G | last post by:
I am a little surprised that the following that $x is visible outside of the scope in which it is (?)defined(?) (not sure if that is the correct term here). I am trying to find where in the php...
134
7750
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...
166
8481
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
14868
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...
6
14152
by: Jody Gelowitz | last post by:
I have run into an issue with variable scope within an XSLT document that is translated in VS.NET 2.0. Under VS.NET 1.1 (XslTransform), this code works fine. However, when using VS.NET 2.0...
5
2258
by: strawberry | last post by:
In the function below, I'd like to extend the scope of the $table variable such that, once assigned it would become available to other parts of the function. I thought 'global $table;' would solve...
112
5351
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...
2
2889
by: ray | last post by:
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,...
0
7257
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7157
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7379
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7535
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7098
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...
0
7521
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...
1
5084
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...
0
3221
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1591
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 ...

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.