473,406 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 1882
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
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
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
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
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
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
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
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
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
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
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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,...
0
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...

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.