Question

how long are php superglobals stored?

Let's take this for example:

<?php 
$x = 75; 
$y = 25;

function addition() { 
  $GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y']; 
}

addition(); 
echo $z; 
?>

How long will the variables be accessible to the application? One day, one month, one year? Forever?

I'm creating a multiplayer game which is a php web application. I'm just trying to figure out the best way to store persistent data.

I'm thinking mysql is my best option right now. In coldfusion you have different variable scopes, i.e. session variables vs application variables.

Thank you.

B

Was it helpful?

Solution

Globals are not global to your entire server nor to every script that is running on it. They are only global to one, single instance of a script. Not even two instances of the same script can access each other's globals. So, once that one instance is done running, those globals are gone. The next time the script runs, it won't have those globals anymore.

If you want to keep something for use between instances, you'll have to use a database, sessions, a file, etc. Though, sessions will only keep them around on a per-user basis, not globally across instances of the script.

If you're making a game, a database would probably be your best solution. They are made to store data and to get it back using queries. That means it'll be pretty easy for you to store data and get back the data you want.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top