Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Global variables

0 views
Skip to first unread message

Charles O'Flynn

unread,
Sep 26, 2006, 3:24:45 PM9/26/06
to
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.


Johnny

unread,
Sep 26, 2006, 3:46:39 PM9/26/06
to

"Charles O'Flynn" <cha...@matchwalk.com> wrote in message
news:12hivk1...@corp.supernews.com...

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


Charles O'Flynn

unread,
Sep 26, 2006, 4:37:35 PM9/26/06
to
"Johnny" <removethi...@hotmail.com> wrote in message
news:lsfSg.375$UJ2.143@fed1read07...
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


Charles O'Flynn

unread,
Sep 26, 2006, 5:05:38 PM9/26/06
to

"Charles O'Flynn" <cha...@matchwalk.com> wrote in message
news:12hj3sq...@corp.supernews.com...
********************************************************
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?


Jerry Stuckle

unread,
Sep 26, 2006, 5:07:40 PM9/26/06
to

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.
jstu...@attglobal.net
==================

Charles O'Flynn

unread,
Sep 26, 2006, 5:52:37 PM9/26/06
to
"Jerry Stuckle" <jstu...@attglobal.net> wrote in message
news:ytCdnSaj__sJC4TY...@comcast.com...
| > $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.
| jstu...@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...


Oli Filth

unread,
Sep 26, 2006, 7:23:55 PM9/26/06
to
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.variables.scope.php#language.variables.scope.global

>
> global $variable;
>
> function printsomething()
> {
> global $variable;
>
> $variable = 'Test'.<b />;
> echo $variable;
> }
>
> printsomething();
> echo $variable;
>


--
Oli

Oli Filth

unread,
Sep 26, 2006, 7:29:01 PM9/26/06
to
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

Charles O'Flynn

unread,
Sep 27, 2006, 3:28:00 AM9/27/06
to

"Oli Filth" <ca...@olifilth.co.uk> wrote in message
news:fEiSg.32264$TF5....@newsfe1-win.ntli.net...


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


Charles O'Flynn

unread,
Sep 27, 2006, 3:54:24 AM9/27/06
to
"Charles O'Flynn" <cha...@matchwalk.com> wrote in message
news:12hka05...@corp.supernews.com...


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


Norman Peelman

unread,
Sep 27, 2006, 7:04:16 AM9/27/06
to
"Charles O'Flynn" <cha...@matchwalk.com> wrote in message
news:12hkbhi...@corp.supernews.com...

Of course, there's always the $_GLOBALS array...

Norm


0 new messages