Reply to Re: how do you initialize global variables once?

Your name:

Reply:


Posted by Erwin Moller on 03/23/06 14:59

Stephen Kay wrote:

> Sorry fi this is a stupid question - I come from a c/c++ background, and
> the global variables in php aren't making sense to me.
>
> If I want to declare a global variable, say in a global.php file, and have
> it initialized to a certian value once, can that be done? Example:
>
> // in global.php
>
> $myvar = 25;
>
> // in some other file
>
> require ("global.php");
>
> function somefunction()
> {
> global $myvar;
>
> if ($myvar == 25){
> $myvar = 30;
> echo "setting myvar to 30.";
> }
> }
>
>
> So far, so good. I can access this variable in this other functions.
>
> But it doesn't seem to *stay* 30. It goes back to 25. Must be some other
> way to do this in php, but I haven't come across the documentation yet.
> Thanks for any info.
>
>
>


Hi,

If you define some var in a file, global or not, and include that file
before another, you are just 'copy and pasting' some code in PHP.

So your example just initializes the var $myvar every time again.
That is why you cannot change it after the script is finished.
Simply because the file you include is not changed, so it is 25 next time
you include it above another script.

Remember that PHP is per-request, and doesn't have some runtime running in
the background where you can store 'real' globals.
(Actually in some situations, it does, but let's forget about that because
it is not helping you.)

So how do you remember a value from script to the next script?

You can do that in several ways, depending on your requirements.

Some ideas:
1) You want to remember a value for one client. Use Session. (It uses a
unique PHPSESSID passed by cookie or URL so the server can identify you
next time you hit a page.)

2) You want a global value, used for ALL your scripts on your site.
If you want this value to be the same all the time, just use an include as
you did in your previous code. That means you are stuck with the value and
cannot ever change it.

3) You want a global value available to ALL your scripts AND want to change
it by means of the PHP code.
I think that by far the easiest way to do this is by storing and retrieving
the value somewhere, in a file (using filelocking) or in a database.

AFAIK there is no such thing as Servletcontext (Java J2EE) or APPLICATION
(VB/ASP script) in PHP.


Hope that helps.

What situation do you have that you need this anyway?
(Just curious)

Regards,
Erwin Moller

[Back to original 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

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