473,795 Members | 2,605 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Variable scope mystery

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
<?php
function blah()
{
include("script 2.php");
}

blah();
?>

script2.php
<?php
$foo = "bar";

function blahblah()
{
global $foo;
echo $foo;
}

blahblah();
?>

I can't see anything wrong with the way I set this up, even more since a
call to script2.php does exactly what it's suppossed to do: echo the value.
What's going on here is beyond me. Anybody has a clue?

Kind regards, Maarten
Nov 22 '05 #1
6 1480
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Maarten wrote:
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
<?php
function blah()
{
include("script 2.php");
}

blah();
?>

script2.php
<?php
$foo = "bar";

function blahblah()
{
global $foo;
echo $foo;
}

blahblah();
?>

I can't see anything wrong with the way I set this up, even more since a
call to script2.php does exactly what it's suppossed to do: echo the value.
What's going on here is beyond me. Anybody has a clue?

Kind regards, Maarten


I can... you can't expect to include another function within blah();

To do it, use a class, and include your blahblah(); and just use blah();
to reference to it within your class::function ();
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (MingW32)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFDfmZh/WE0aXnOUiYRAmpQ AJ4gbRQBepMOTri ilvr82GryQuykTQ Cgk0TZ
tZ637ZaemoIUKhM 4n4Frh2w=
=f1n2
-----END PGP SIGNATURE-----
Nov 22 '05 #2
daemon said the following on 18/11/2005 16:43:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Maarten wrote:
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
<?php
function blah()
{
include("script 2.php");
}

blah();
?>

script2.php
<?php
$foo = "bar";

function blahblah()
{
global $foo;
echo $foo;
}

blahblah();
?>

I can't see anything wrong with the way I set this up, even more since a
call to script2.php does exactly what it's suppossed to do: echo the value.
What's going on here is beyond me. Anybody has a clue?

Kind regards, Maarten

I can... you can't expect to include another function within blah();


Yes you can.

The problem is that by declaring $foo = "bar" in what intuitively looks
like the global scope within script2.php, it's actually being declared
in the scope of blah(), and therefore isn't a global variable, and
therefore can't be accessed within blahblah().

Change script2.php to:

<?php
global $foo;
$foo = "bar";

function blahblah()
{
global $foo;
echo $foo;
}

blahblah();
?>

--
Oli
Nov 22 '05 #3
Thank you for replying, but you are mistaking. This for example works fine,
even when the second function is included from an outside file.

function blah()
{
function blahblah()
{
echo "foo";
}
blahblah();
}
blah();

"daemon" <d4****@shaw.ca > wrote in message
news:cxnff.5236 97$oW2.516262@p d7tw1no...
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Maarten wrote:
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
<?php
function blah()
{
include("script 2.php");
}

blah();
?>

script2.php
<?php
$foo = "bar";

function blahblah()
{
global $foo;
echo $foo;
}

blahblah();
?>

I can't see anything wrong with the way I set this up, even more since a
call to script2.php does exactly what it's suppossed to do: echo the
value.
What's going on here is beyond me. Anybody has a clue?

Kind regards, Maarten


I can... you can't expect to include another function within blah();

To do it, use a class, and include your blahblah(); and just use blah();
to reference to it within your class::function ();
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (MingW32)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFDfmZh/WE0aXnOUiYRAmpQ AJ4gbRQBepMOTri ilvr82GryQuykTQ Cgk0TZ
tZ637ZaemoIUKhM 4n4Frh2w=
=f1n2
-----END PGP SIGNATURE-----

Nov 22 '05 #4
Yes, that's it! So, a variable declared inside a function isn't available
inside nested function even while you are calling it in (global). You'll
have to declare it global in the main function first. This can cause
problems though, as there might be a variable with the same name outside the
main function that you do not want to access. See what I mean? The solution
is obvious of course, but sometimes you just don't know what variables live
out there, do you?
"Oli Filth" <ca***@olifilth .co.uk> wrote in message
news:YT******** **********@news fe6-gui.ntli.net...
daemon said the following on 18/11/2005 16:43:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Maarten wrote:
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.ph p
<?php
function blah()
{
include("script 2.php");
}

blah();
?>

script2.ph p
<?php
$foo = "bar";

function blahblah()
{
global $foo;
echo $foo;
}

blahblah() ;
?>

I can't see anything wrong with the way I set this up, even more since a
call to script2.php does exactly what it's suppossed to do: echo the
value. What's going on here is beyond me. Anybody has a clue?

Kind regards, Maarten

I can... you can't expect to include another function within blah();


Yes you can.

The problem is that by declaring $foo = "bar" in what intuitively looks
like the global scope within script2.php, it's actually being declared in
the scope of blah(), and therefore isn't a global variable, and therefore
can't be accessed within blahblah().

Change script2.php to:

<?php
global $foo;
$foo = "bar";

function blahblah()
{
global $foo;
echo $foo;
}

blahblah();
?>

--
Oli

Nov 22 '05 #5
Maarten said the following on 18/11/2005 17:28:
Yes, that's it! So, a variable declared inside a function isn't available
inside nested function even while you are calling it in (global). You'll
have to declare it global in the main function first. This can cause
problems though, as there might be a variable with the same name outside the
main function that you do not want to access. See what I mean? The solution
is obvious of course, but sometimes you just don't know what variables live
out there, do you?


I think the solution is: Don't use global variables!

Always explicitly pass required variables to functions.

--
Oli
Nov 22 '05 #6
What can I say? You are right of course. Thanks you very much for your help.

Regards, Maarten

"Oli Filth" <ca***@olifilth .co.uk> wrote in message
news:Lb******** **********@news fe6-gui.ntli.net...
Maarten said the following on 18/11/2005 17:28:
Yes, that's it! So, a variable declared inside a function isn't available
inside nested function even while you are calling it in (global). You'll
have to declare it global in the main function first. This can cause
problems though, as there might be a variable with the same name outside
the main function that you do not want to access. See what I mean? The
solution is obvious of course, but sometimes you just don't know what
variables live out there, do you?


I think the solution is: Don't use global variables!

Always explicitly pass required variables to functions.

--
Oli

Nov 22 '05 #7

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

Similar topics

3
2018
by: Grant Wagner | last post by:
Given the following working code: function attributes() { var attr1 = arguments || '_'; var attr2 = arguments || '_'; return ( function (el1, el2) { var value1 = el1 + el1; var value2 = el2 + el2; if (value1 > value2) return 1;
4
14906
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...
2
987
by: Laurence Nuttall | last post by:
I have a class, and a public varible defined in one of the ..VB files of that class. I also have a form in that class, but I cannot reference the variable in the class from the form, even though the form is in the class. Thanks in Advance, Laurence Nuttall
23
19205
by: Russ Chinoy | last post by:
Hi, This may be a totally newbie question, but I'm stumped. If I have a function such as: function DoSomething(strVarName) { ..... }
1
25702
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...
0
35249
MMcCarthy
by: MMcCarthy | last post by:
We often get questions on this site that refer to the scope of variables and where and how they are declared. This tutorial is intended to cover the basics of variable scope in VBA for MS Access. For the sake of brevity I am sticking to common usage. Wherever the term procedure is used in this tutorial it actually refers to a subroutine or function. Definition of Scope The scope of a variable where this variable can be seen or accessed...
2
4718
by: Shraddha | last post by:
Can we declare extern variable as static? What will be the scope of the variable then? What if we change the value of the variable in some other function? Also can someone tell me that if we can declare the global variable...and it is having scope throughout the file then what is so different in extern variable???
7
6685
by: pauldepstein | last post by:
#include <iostream> using namespace std; double & GetWeeklyHours() { double h = 46.50; double &hours = h; return hours; } //---------------------------------------------------------------------------
112
5485
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)
0
9672
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9519
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10163
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
9040
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
7538
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
5436
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
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
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
3722
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.