just need to use the global keyword when inside the function, it's explained
here:
http://us3.php.net/global
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
<?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?
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
==================
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...
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
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
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