473,467 Members | 1,603 Online
Bytes | Software Development & Data Engineering Community
Create 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("script2.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 1453
-----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("script2.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/WE0aXnOUiYRAmpQAJ4gbRQBepMOTriilvr82GryQuykTQCgk0T Z
tZ637ZaemoIUKhM4n4Frh2w=
=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("script2.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.523697$oW2.516262@pd7tw1no...
-----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("script2.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/WE0aXnOUiYRAmpQAJ4gbRQBepMOTriilvr82GryQuykTQCgk0T Z
tZ637ZaemoIUKhM4n4Frh2w=
=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******************@newsfe6-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.php
<?php
function blah()
{
include("script2.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 #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******************@newsfe6-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
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...
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...
2
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...
23
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
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...
0
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...
2
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...
7
by: pauldepstein | last post by:
#include <iostream> using namespace std; double & GetWeeklyHours() { double h = 46.50; double &hours = h; return hours; }...
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...
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
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
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
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
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
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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.