For alot of values, is $_SESSION more efficient?
For alot of values, is $_SESSION more efficient?
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?
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
Driftwurld : My Browser Game WIP
Philosopher Gamer : My Blog
- Jackolantern
- Posts: 10891
- Joined: Wed Jul 01, 2009 11:00 pm
Re: For alot of values, is $_SESSION more efficient?
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
- hallsofvallhalla
- Site Admin
- Posts: 12026
- Joined: Wed Apr 22, 2009 11:29 pm
Re: For alot of values, is $_SESSION more efficient?
and what kinda data u wanting to save?
Re: For alot of values, is $_SESSION more efficient?
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
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)'
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";Fight Cycle : My latest Browser game WIP
Driftwurld : My Browser Game WIP
Philosopher Gamer : My Blog
Driftwurld : My Browser Game WIP
Philosopher Gamer : My Blog
- hallsofvallhalla
- Site Admin
- Posts: 12026
- Joined: Wed Apr 22, 2009 11:29 pm
Re: For alot of values, is $_SESSION more efficient?
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.
- Jackolantern
- Posts: 10891
- Joined: Wed Jul 01, 2009 11:00 pm
Re: For alot of values, is $_SESSION more efficient?
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:
And then just include the file at the top of any other script that needs access to those constants:
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!
Code: Select all
<?php
define('DARK_FOREST', 0);
define('FOREST', 1);
define('ANCIENT_CITY_RUINS', 2);
//...and so on...
?>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...";
}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
- hallsofvallhalla
- Site Admin
- Posts: 12026
- Joined: Wed Apr 22, 2009 11:29 pm
Re: For alot of values, is $_SESSION more efficient?
ohhh good idea Jack.
Re: For alot of values, is $_SESSION more efficient?
I'm not sure I understand the structure there
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?
Code: Select all
checkForDeathDuringTravel(ANCIENT_CITY_RUINS);
function checkForDeathDuringTravel($wentTo) {Fight Cycle : My latest Browser game WIP
Driftwurld : My Browser Game WIP
Philosopher Gamer : My Blog
Driftwurld : My Browser Game WIP
Philosopher Gamer : My Blog
- Jackolantern
- Posts: 10891
- Joined: Wed Jul 01, 2009 11:00 pm
Re: For alot of values, is $_SESSION more efficient?
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