|
Posted by Charles O'Flynn on 09/26/06 21:04
"Charles O'Flynn" <charles@matchwalk.com> wrote in message
news:12hj1pu2q1pv842@corp.supernews.com...
|
| "Johnny" <removethis.huuanito@hotmail.com> wrote in message
| news:lsfSg.375$UJ2.143@fed1read07...
||
|| "Charles O'Flynn" <charles@matchwalk.com> wrote in message
|| news:12hivk1109pj1e0@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
|
********************************************************
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?
Navigation:
[Reply to this message]
|