By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,799 Members | 1,356 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,799 IT Pros & Developers. It's quick & easy.

Global variables

P: n/a
As a complete newcomer (2-3 days) to PHP, although not to programming in
general, I have 'dived in' to start a small project to read and parse an XML
data stream. I have already worked out most of the more specialist aspects
of the job but am now completely stuck on something I would have thought
were simplicity itself...
I need to have a large number of global variables visible inside functions -
it's not possible to pass them into the functions themselves, since although
they are user functions, the parameter type/count is fixed. Reading what
information I can find, I was under the impression that variables declared
at the head of the PHP block as 'global' would be visible inside all
functions. My problem is this: yes, it appears I can assign values to
these global variables inside a function, (I think), but immediately I exit
the function, the data is lost. At first sight, I could be assigning values
to variables with identical names but local scope within the functions, but
when I performed an 'explode()' inside a function, assigning the result to
one of my 'global' variables and then, on exiting the function, tried to
'echo' the result, the result was 'ARRAY' - the original global variable had
presumably been converted, since it started life as a scalar.
I don't really want to go to superglobals unless I have to - can anybody
please tell me where I'm going so obviously wrong and how I can correct it?
This is such a basic problem, I can't help thinking everybody must know the
answer...
Thanks for any help/advice offered.
Sep 26 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a

"Charles O'Flynn" <ch*****@matchwalk.comwrote in message
news:12*************@corp.supernews.com...
As a complete newcomer (2-3 days) to PHP, although not to programming in
general, I have 'dived in' to start a small project to read and parse an
XML
data stream. I have already worked out most of the more specialist
aspects
of the job but am now completely stuck on something I would have thought
were simplicity itself...
I need to have a large number of global variables visible inside
functions -
it's not possible to pass them into the functions themselves, since
although
they are user functions, the parameter type/count is fixed. Reading what
information I can find, I was under the impression that variables declared
at the head of the PHP block as 'global' would be visible inside all
functions. My problem is this: yes, it appears I can assign values to
these global variables inside a function, (I think), but immediately I
exit
the function, the data is lost. At first sight, I could be assigning
values
to variables with identical names but local scope within the functions,
but
when I performed an 'explode()' inside a function, assigning the result to
one of my 'global' variables and then, on exiting the function, tried to
'echo' the result, the result was 'ARRAY' - the original global variable
had
presumably been converted, since it started life as a scalar.
I don't really want to go to superglobals unless I have to - can anybody
please tell me where I'm going so obviously wrong and how I can correct
it?
This is such a basic problem, I can't help thinking everybody must know
the
answer...
Thanks for any help/advice offered.

just need to use the global keyword when inside the function, it's explained
here:
http://us3.php.net/global
Sep 26 '06 #2

P: n/a
"Johnny" <re*****************@hotmail.comwrote in message
news:lsfSg.375$UJ2.143@fed1read07...
|
| "Charles O'Flynn" <ch*****@matchwalk.comwrote in message
| news:12*************@corp.supernews.com...
| As a complete newcomer (2-3 days) to PHP, although not to programming in
| general, I have 'dived in' to start a small project to read and parse an
| XML
| data stream. I have already worked out most of the more specialist
| aspects
| of the job but am now completely stuck on something I would have thought
| were simplicity itself...
| I need to have a large number of global variables visible inside
| functions -
| it's not possible to pass them into the functions themselves, since
| although
| they are user functions, the parameter type/count is fixed. Reading
what
| information I can find, I was under the impression that variables
declared
| at the head of the PHP block as 'global' would be visible inside all
| functions. My problem is this: yes, it appears I can assign values to
| these global variables inside a function, (I think), but immediately I
| exit
| the function, the data is lost. At first sight, I could be assigning
| values
| to variables with identical names but local scope within the functions,
| but
| when I performed an 'explode()' inside a function, assigning the result
to
| one of my 'global' variables and then, on exiting the function, tried to
| 'echo' the result, the result was 'ARRAY' - the original global variable
| had
| presumably been converted, since it started life as a scalar.
| I don't really want to go to superglobals unless I have to - can anybody
| please tell me where I'm going so obviously wrong and how I can correct
| it?
| This is such a basic problem, I can't help thinking everybody must know
| the
| answer...
| Thanks for any help/advice offered.
| >
| >
|
| just need to use the global keyword when inside the function, it's
explained
| here:
| http://us3.php.net/global
|
Thanks for the quick reply, Johnny, but I've been looking at the page you
refer to all afternoon and it doesn't seem to work for me. For instance,
(and I'm only illustrating the specific problem I seem to have hereunder)...
------------------------------------
$variable;

function printsomething()
{
global $variable;

$variable = 'Test'.<b />;
echo $variable;
}

printsomething();
echo $variable;
------------------------------------

....only prints one line of 'Test' - I'd have thought it should print out two
copies. BTW, I'm running under PHP 4.1.2 (and it's not mine to
change/upgrade!)
Thanks and regards,
Charles
Sep 26 '06 #3

P: n/a

"Charles O'Flynn" <ch*****@matchwalk.comwrote in message
news:12*************@corp.supernews.com...
| "Johnny" <re*****************@hotmail.comwrote in message
| news:lsfSg.375$UJ2.143@fed1read07...
||
|| "Charles O'Flynn" <ch*****@matchwalk.comwrote in message
|| news:12*************@corp.supernews.com...
|| As a complete newcomer (2-3 days) to PHP, although not to programming
in
|| general, I have 'dived in' to start a small project to read and parse
an
|| XML
|| data stream. I have already worked out most of the more specialist
|| aspects
|| of the job but am now completely stuck on something I would have
thought
|| were simplicity itself...
|| I need to have a large number of global variables visible inside
|| functions -
|| it's not possible to pass them into the functions themselves, since
|| although
|| they are user functions, the parameter type/count is fixed. Reading
| what
|| information I can find, I was under the impression that variables
| declared
|| at the head of the PHP block as 'global' would be visible inside all
|| functions. My problem is this: yes, it appears I can assign values to
|| these global variables inside a function, (I think), but immediately I
|| exit
|| the function, the data is lost. At first sight, I could be assigning
|| values
|| to variables with identical names but local scope within the functions,
|| but
|| when I performed an 'explode()' inside a function, assigning the result
| to
|| one of my 'global' variables and then, on exiting the function, tried
to
|| 'echo' the result, the result was 'ARRAY' - the original global
variable
|| had
|| presumably been converted, since it started life as a scalar.
|| I don't really want to go to superglobals unless I have to - can
anybody
|| please tell me where I'm going so obviously wrong and how I can correct
|| it?
|| This is such a basic problem, I can't help thinking everybody must know
|| the
|| answer...
|| Thanks for any help/advice offered.
|| >
|| >
||
|| just need to use the global keyword when inside the function, it's
| explained
|| here:
|| http://us3.php.net/global
||
| Thanks for the quick reply, Johnny, but I've been looking at the page you
| refer to all afternoon and it doesn't seem to work for me. For instance,
| (and I'm only illustrating the specific problem I seem to have
hereunder)...
| ------------------------------------
| $variable;
|
| function printsomething()
| {
| global $variable;
|
| $variable = 'Test'.<b />;
| echo $variable;
| }
|
| printsomething();
| echo $variable;
| ------------------------------------
|
| ...only prints one line of 'Test' - I'd have thought it should print out
two
| copies. BTW, I'm running under PHP 4.1.2 (and it's not mine to
| change/upgrade!)
| Thanks and regards,
| Charles
|
************************************************** ******
I thought I'd just try out what I wrote above and loaded this up...

<?php
$loc_place;

function writesomething()
{
global $loc_place;

$loc_place = 'This is a test';
echo $loc_place.'<br />';
}

echo $loc_place.' again<br />';
writesomething();
?>

As I wrote earlier, I get no sign of any output from outside the function,
only inside...

This is a test

Surely this cannot be right?
Sep 26 '06 #4

P: n/a
Charles O'Flynn wrote:
"Johnny" <re*****************@hotmail.comwrote in message
news:lsfSg.375$UJ2.143@fed1read07...
|
| "Charles O'Flynn" <ch*****@matchwalk.comwrote in message
| news:12*************@corp.supernews.com...
| As a complete newcomer (2-3 days) to PHP, although not to programming in
| general, I have 'dived in' to start a small project to read and parse an
| XML
| data stream. I have already worked out most of the more specialist
| aspects
| of the job but am now completely stuck on something I would have thought
| were simplicity itself...
| I need to have a large number of global variables visible inside
| functions -
| it's not possible to pass them into the functions themselves, since
| although
| they are user functions, the parameter type/count is fixed. Reading
what
| information I can find, I was under the impression that variables
declared
| at the head of the PHP block as 'global' would be visible inside all
| functions. My problem is this: yes, it appears I can assign values to
| these global variables inside a function, (I think), but immediately I
| exit
| the function, the data is lost. At first sight, I could be assigning
| values
| to variables with identical names but local scope within the functions,
| but
| when I performed an 'explode()' inside a function, assigning the result
to
| one of my 'global' variables and then, on exiting the function, tried to
| 'echo' the result, the result was 'ARRAY' - the original global variable
| had
| presumably been converted, since it started life as a scalar.
| I don't really want to go to superglobals unless I have to - can anybody
| please tell me where I'm going so obviously wrong and how I can correct
| it?
| This is such a basic problem, I can't help thinking everybody must know
| the
| answer...
| Thanks for any help/advice offered.
| >
| >
|
| just need to use the global keyword when inside the function, it's
explained
| here:
| http://us3.php.net/global
|
Thanks for the quick reply, Johnny, but I've been looking at the page you
refer to all afternoon and it doesn't seem to work for me. For instance,
(and I'm only illustrating the specific problem I seem to have hereunder)...
------------------------------------
$variable;

function printsomething()
{
global $variable;

$variable = 'Test'.<b />;
echo $variable;
}

printsomething();
echo $variable;
------------------------------------

...only prints one line of 'Test' - I'd have thought it should print out two
copies. BTW, I'm running under PHP 4.1.2 (and it's not mine to
change/upgrade!)
Thanks and regards,
Charles

You're close. But you have to use the global keyword in the global
context, also. Not just in the function.

global $variable;

function printsomething()
{
global $variable;

$variable = 'Test'.<b />;
echo $variable;
}

printsomething();
echo $variable;

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Sep 26 '06 #5

P: n/a
"Jerry Stuckle" <js*******@attglobal.netwrote in message
news:yt******************************@comcast.com. ..
| Charles O'Flynn wrote:
| "Johnny" <re*****************@hotmail.comwrote in message
| news:lsfSg.375$UJ2.143@fed1read07...
| |
| | "Charles O'Flynn" <ch*****@matchwalk.comwrote in message
| | news:12*************@corp.supernews.com...
| | As a complete newcomer (2-3 days) to PHP, although not to
programming in
| | general, I have 'dived in' to start a small project to read and
parse an
| | XML
| | data stream. I have already worked out most of the more specialist
| | aspects
| | of the job but am now completely stuck on something I would have
thought
| | were simplicity itself...
| | I need to have a large number of global variables visible inside
| | functions -
| | it's not possible to pass them into the functions themselves, since
| | although
| | they are user functions, the parameter type/count is fixed. Reading
| what
| | information I can find, I was under the impression that variables
| declared
| | at the head of the PHP block as 'global' would be visible inside all
| | functions. My problem is this: yes, it appears I can assign values
to
| | these global variables inside a function, (I think), but immediately
I
| | exit
| | the function, the data is lost. At first sight, I could be
assigning
| | values
| | to variables with identical names but local scope within the
functions,
| | but
| | when I performed an 'explode()' inside a function, assigning the
result
| to
| | one of my 'global' variables and then, on exiting the function,
tried to
| | 'echo' the result, the result was 'ARRAY' - the original global
variable
| | had
| | presumably been converted, since it started life as a scalar.
| | I don't really want to go to superglobals unless I have to - can
anybody
| | please tell me where I'm going so obviously wrong and how I can
correct
| | it?
| | This is such a basic problem, I can't help thinking everybody must
know
| | the
| | answer...
| | Thanks for any help/advice offered.
| | >
| | >
| |
| | just need to use the global keyword when inside the function, it's
| explained
| | here:
| | http://us3.php.net/global
| |
| Thanks for the quick reply, Johnny, but I've been looking at the page
you
| refer to all afternoon and it doesn't seem to work for me. For
instance,
| (and I'm only illustrating the specific problem I seem to have
hereunder)...
| ------------------------------------
| $variable;
| >
| function printsomething()
| {
| global $variable;
| >
| $variable = 'Test'.<br />;
| echo $variable;
| }
| >
| printsomething();
| echo $variable;
| ------------------------------------
| >
| ...only prints one line of 'Test' - I'd have thought it should print out
two
| copies. BTW, I'm running under PHP 4.1.2 (and it's not mine to
| change/upgrade!)
| Thanks and regards,
| Charles
| >
| >
|
| You're close. But you have to use the global keyword in the global
| context, also. Not just in the function.
|
| global $variable;
|
| function printsomething()
| {
| global $variable;
|
| $variable = 'Test'.<br />;
| echo $variable;
| }
|
| printsomething();
| echo $variable;
|
| --
| ==================
| Remove the "x" from my email address
| Jerry Stuckle
| JDS Computer Training Corp.
| js*******@attglobal.net
| ==================

Thanks, but I've tried that and it still doesn't work.
I'm getting the feeling PHP doesn't like me, although I like it well
enough...
Sep 26 '06 #6

P: n/a
Jerry Stuckle said the following on 26/09/2006 22:07:
Charles O'Flynn wrote:
>Thanks for the quick reply, Johnny, but I've been looking at the page you
refer to all afternoon and it doesn't seem to work for me. For instance,
(and I'm only illustrating the specific problem I seem to have
hereunder)...
------------------------------------
$variable;

function printsomething()
{
global $variable;

$variable = 'Test'.<b />;
echo $variable;
}

printsomething();
echo $variable;
------------------------------------

...only prints one line of 'Test' - I'd have thought it should print
out two
copies. BTW, I'm running under PHP 4.1.2 (and it's not mine to
change/upgrade!)

You're close. But you have to use the global keyword in the global
context, also. Not just in the function.
Umm, no you don't!
http://uk.php.net/manual/en/language...s.scope.global
>
global $variable;

function printsomething()
{
global $variable;

$variable = 'Test'.<b />;
echo $variable;
}

printsomething();
echo $variable;

--
Oli
Sep 26 '06 #7

P: n/a
Charles O'Flynn said the following on 26/09/2006 22:05:
I thought I'd just try out what I wrote above and loaded this up...

<?php
$loc_place;

function writesomething()
{
global $loc_place;

$loc_place = 'This is a test';
echo $loc_place.'<br />';
}

echo $loc_place.' again<br />';
writesomething();
?>

As I wrote earlier, I get no sign of any output from outside the function,
only inside...

This is a test
Well, in this code, $loc_place has no value assigned before
writesomething() is called. Depending on your error settings and
version, I guess the echo $loc_place.' again<br />' line might fail
silently; although in PHP 5 it just echoes " again<br />". I don't have
PHP 4 running to test the behaviour.
--
Oli
Sep 26 '06 #8

P: n/a

"Oli Filth" <ca***@olifilth.co.ukwrote in message
news:fE******************@newsfe1-win.ntli.net...
| Jerry Stuckle said the following on 26/09/2006 22:07:
| Charles O'Flynn wrote:
| >Thanks for the quick reply, Johnny, but I've been looking at the page
you
| >refer to all afternoon and it doesn't seem to work for me. For
instance,
| >(and I'm only illustrating the specific problem I seem to have
| >hereunder)...
| >------------------------------------
| >$variable;
| >>
| >function printsomething()
| >{
| > global $variable;
| >>
| > $variable = 'Test'.<b />;
| > echo $variable;
| >}
| >>
| >printsomething();
| >echo $variable;
| >------------------------------------
| >>
| >...only prints one line of 'Test' - I'd have thought it should print
| >out two
| >copies. BTW, I'm running under PHP 4.1.2 (and it's not mine to
| >change/upgrade!)
| >
| You're close. But you have to use the global keyword in the global
| context, also. Not just in the function.
|
| Umm, no you don't!
|
http://uk.php.net/manual/en/language...s.scope.global
|
| >
| global $variable;
| >
| function printsomething()
| {
| global $variable;
| >
| $variable = 'Test'.<b />;
| echo $variable;
| }
| >
| printsomething();
| echo $variable;
| >
|
|
| --
| Oli
Thanks, Oli
I'm getting the feeling, (although noone's spelling it out either here or in
any of the myriad books I've looked at for inspiration), that declaring a
variable as global inside a function will make it accessible outside the
function at the global scope; in other words, what I've done above is
declare two independent variables, the one outside the function over-riding
the effect of the one inside the function.. OK - I can test this very
quickly. But if so, how on earth do I get to access it within another
function, or does this automatically make it visible everywhere?
Of course, I could store the data within MySQL, thereby making it
persistent, but this seems like overkill. How does PHP make variables
accessible with 'real' global scope, not just 'global, except inside
functions', which for an old 'C' programmer like me, is not global at all?
I know, in theory about superglobals but again, this seems like overkill.
Or am I being silly?
Thanks,
Charles
Sep 27 '06 #9

P: n/a
"Charles O'Flynn" <ch*****@matchwalk.comwrote in message
news:12*************@corp.supernews.com...
|
| "Oli Filth" <ca***@olifilth.co.ukwrote in message
| news:fE******************@newsfe1-win.ntli.net...
|| Jerry Stuckle said the following on 26/09/2006 22:07:
|| Charles O'Flynn wrote:
|| >Thanks for the quick reply, Johnny, but I've been looking at the page
| you
|| >refer to all afternoon and it doesn't seem to work for me. For
| instance,
|| >(and I'm only illustrating the specific problem I seem to have
|| >hereunder)...
|| >------------------------------------
|| >$variable;
|| >>
|| >function printsomething()
|| >{
|| > global $variable;
|| >>
|| > $variable = 'Test'.<b />;
|| > echo $variable;
|| >}
|| >>
|| >printsomething();
|| >echo $variable;
|| >------------------------------------
|| >>
|| >...only prints one line of 'Test' - I'd have thought it should print
|| >out two
|| >copies. BTW, I'm running under PHP 4.1.2 (and it's not mine to
|| >change/upgrade!)
|| >
|| You're close. But you have to use the global keyword in the global
|| context, also. Not just in the function.
||
|| Umm, no you don't!
||
|
http://uk.php.net/manual/en/language...s.scope.global
||
|| >
|| global $variable;
|| >
|| function printsomething()
|| {
|| global $variable;
|| >
|| $variable = 'Test'.<b />;
|| echo $variable;
|| }
|| >
|| printsomething();
|| echo $variable;
|| >
||
||
|| --
|| Oli
|
|
| Thanks, Oli
| I'm getting the feeling, (although noone's spelling it out either here or
in
| any of the myriad books I've looked at for inspiration), that declaring a
| variable as global inside a function will make it accessible outside the
| function at the global scope; in other words, what I've done above is
| declare two independent variables, the one outside the function
over-riding
| the effect of the one inside the function.. OK - I can test this very
| quickly. But if so, how on earth do I get to access it within another
| function, or does this automatically make it visible everywhere?
| Of course, I could store the data within MySQL, thereby making it
| persistent, but this seems like overkill. How does PHP make variables
| accessible with 'real' global scope, not just 'global, except inside
| functions', which for an old 'C' programmer like me, is not global at all?
| I know, in theory about superglobals but again, this seems like overkill.
| Or am I being silly?
| Thanks,
| Charles
|
Problem now solved (from the point of view of this specific query). Thanks
to Oli, Norm and Johnny for taking the trouble to reply.
Regards,
Charles
Sep 27 '06 #10

P: n/a
"Charles O'Flynn" <ch*****@matchwalk.comwrote in message
news:12*************@corp.supernews.com...
"Charles O'Flynn" <ch*****@matchwalk.comwrote in message
news:12*************@corp.supernews.com...
|
| "Oli Filth" <ca***@olifilth.co.ukwrote in message
| news:fE******************@newsfe1-win.ntli.net...
|| Jerry Stuckle said the following on 26/09/2006 22:07:
|| Charles O'Flynn wrote:
|| >Thanks for the quick reply, Johnny, but I've been looking at the
page
| you
|| >refer to all afternoon and it doesn't seem to work for me. For
| instance,
|| >(and I'm only illustrating the specific problem I seem to have
|| >hereunder)...
|| >------------------------------------
|| >$variable;
|| >>
|| >function printsomething()
|| >{
|| > global $variable;
|| >>
|| > $variable = 'Test'.<b />;
|| > echo $variable;
|| >}
|| >>
|| >printsomething();
|| >echo $variable;
|| >------------------------------------
|| >>
|| >...only prints one line of 'Test' - I'd have thought it should print
|| >out two
|| >copies. BTW, I'm running under PHP 4.1.2 (and it's not mine to
|| >change/upgrade!)
|| >
|| You're close. But you have to use the global keyword in the global
|| context, also. Not just in the function.
||
|| Umm, no you don't!
||
|
http://uk.php.net/manual/en/language...s.scope.global
||
|| >
|| global $variable;
|| >
|| function printsomething()
|| {
|| global $variable;
|| >
|| $variable = 'Test'.<b />;
|| echo $variable;
|| }
|| >
|| printsomething();
|| echo $variable;
|| >
||
||
|| --
|| Oli
|
|
| Thanks, Oli
| I'm getting the feeling, (although noone's spelling it out either here
or
in
| any of the myriad books I've looked at for inspiration), that declaring
a
| variable as global inside a function will make it accessible outside the
| function at the global scope; in other words, what I've done above is
| declare two independent variables, the one outside the function
over-riding
| the effect of the one inside the function.. OK - I can test this very
| quickly. But if so, how on earth do I get to access it within another
| function, or does this automatically make it visible everywhere?
| Of course, I could store the data within MySQL, thereby making it
| persistent, but this seems like overkill. How does PHP make variables
| accessible with 'real' global scope, not just 'global, except inside
| functions', which for an old 'C' programmer like me, is not global at
all?
| I know, in theory about superglobals but again, this seems like
overkill.
| Or am I being silly?
| Thanks,
| Charles
|
Problem now solved (from the point of view of this specific query).
Thanks
to Oli, Norm and Johnny for taking the trouble to reply.
Regards,
Charles
Of course, there's always the $_GLOBALS array...

Norm
Sep 27 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.