You are here: Re: Global variables « All PHP « IT news, forums, messages
Re: Global variables

Posted by Charles O'Flynn on 09/26/06 20:01

"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'.<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

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация