how do you initialize global variables once?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
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.

Stephen Kay

Re: how do you initialize global variables once?

On Thu, 23 Mar 2006 06:16:42 GMT, Stephen Kay

Quoted text here. Click to load it

You're fine, the only adjustment you need to make is that PHP
variables are neither typed nor (necessarily) declared by default.

Quoted text here. Click to load it

At what point in your code does it revert to 25? Do you have a
function somewhere that doesn't pull $myvar from the global scope?

Apologies for the ad below.

*** Free account sponsored by ***
*** Encrypt your Internet usage with a free VPN account from ***

Re: how do you initialize global variables once?

Stephen Kay wrote:

Quoted text here. Click to load it


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)

Erwin Moller

Re: how do you initialize global variables once?

The best way i love is just like this...


return $aux

function test(){
  echo $returnValue;

If you want you can pass a var class or array with values...
It for this kind of features that I LOVE PHP... :)


Site Timeline