For alot of values, is $_SESSION more efficient?

For discussions about game development that does not fit in any of the other topics.
Post Reply
User avatar
Callan S.
Posts: 2042
Joined: Sun Jan 24, 2010 5:43 am

For alot of values, is $_SESSION more efficient?

Post by Callan S. »

I want to have some stuff just put into memory by the php code, and I'm wondering if I should use a bunch of $whatever=0, which will repeat every single time the code is run, or whether I should use $_SESSION, and have it that it only sets them once if they haven't been set. Thus after that the page wont be setting them over and over again each time it refreshes?

Which is more efficient? I'd like to just start using the more efficient one. Or are they both about the same?
Fight Cycle : My latest Browser game WIP
Driftwurld : My Browser Game WIP
Philosopher Gamer : My Blog
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: For alot of values, is $_SESSION more efficient?

Post by Jackolantern »

Where are you getting the values to set the variables? That is the important part. If it is coming from the database, then it might be more efficient to store it in $_SESSION() variables and save it from page-to-page.
The indelible lord of tl;dr
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: For alot of values, is $_SESSION more efficient?

Post by hallsofvallhalla »

and what kinda data u wanting to save?
User avatar
Callan S.
Posts: 2042
Joined: Sun Jan 24, 2010 5:43 am

Re: For alot of values, is $_SESSION more efficient?

Post by Callan S. »

Preset stuff, doesn't need a database as it's not going to be dynamic in any way. Fairly short strings and some basic , small numbers.

Like an example is

Code: Select all

$location[0]="Dark Forest";
$location[1]="Forest";
$location[2]="Ancient City Ruins";
$location[3]="Old, Destroyed Village";
$location[4]="Havesting Fields";
$location[5]="Rocky Cayons";
Basically it'll form a 'if you go to X, you win, but if you choose the other two options, you lose game (for the player it's a memory task of remembering which is X)'
Fight Cycle : My latest Browser game WIP
Driftwurld : My Browser Game WIP
Philosopher Gamer : My Blog
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: For alot of values, is $_SESSION more efficient?

Post by hallsofvallhalla »

Sessions might be the best way though if you already have a database in use for the game then I would make a table to store it.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: For alot of values, is $_SESSION more efficient?

Post by Jackolantern »

Actually, for values that will not change, a nice way to store them could be in constants in an included file. For example, you could make a file like this, called constants.php:

Code: Select all

<?php
    define('DARK_FOREST', 0);
    define('FOREST', 1);
    define('ANCIENT_CITY_RUINS', 2);
    //...and so on...
?>
And then just include the file at the top of any other script that needs access to those constants:

Code: Select all

<?php
include_once("constants.php");

checkForDeathDuringTravel(ANCIENT_CITY_RUINS);

function checkForDeathDuringTravel($wentTo) {
     if ($wentTo > -1 && $wentTo < 3) {
         echo "Oops! Should not have gone there! You died...";
}
While this is a pretty useless usage of constants, you can probably see how they can help with organization, particularly when combined with simple include files. It makes your code much more self-explanatory, while providing you with only one place where you must update the underlying value.

Constants may not be the answer to your problem, because it still depends on exactly how you are wanting to use them, but they are a nice addition to any PHP coder's arsenal!
The indelible lord of tl;dr
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: For alot of values, is $_SESSION more efficient?

Post by hallsofvallhalla »

ohhh good idea Jack.
User avatar
Callan S.
Posts: 2042
Joined: Sun Jan 24, 2010 5:43 am

Re: For alot of values, is $_SESSION more efficient?

Post by Callan S. »

I'm not sure I understand the structure there

Code: Select all

checkForDeathDuringTravel(ANCIENT_CITY_RUINS);

function checkForDeathDuringTravel($wentTo) {
You need to define it as a function? But you define the function after calling it? And $wentto will grave the number defined? I can't really tell what's going on?
Fight Cycle : My latest Browser game WIP
Driftwurld : My Browser Game WIP
Philosopher Gamer : My Blog
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: For alot of values, is $_SESSION more efficient?

Post by Jackolantern »

Sorry, I should have broken those two into separate files (I always define functions in include files). And what do you mean "will grave the number defined". You can simply use the constant where ever you want, and PHP will substitute in the underlying value, which in this case are integers. If you don't need the value to change, it doesn't need to be stored in a variable. All you need is a "handle" for the value in that case, and that is exactly what a constant is :) That way you don't have many "magic numbers" floating around your code, nor unnecessary variables that don't need to be variable.
The indelible lord of tl;dr
Post Reply

Return to “General Development”