[ACCEPTED]-Fastest way to store easily editable config data in PHP?-config

Accepted answer
Score: 43

Serialize is a better option than JSON for 7 storing PHP variables.

I like to use var_export for 6 saving config file, and using include for loading 5 config info. This makes it easy to save 4 config data progmatically AND makes the 3 data easy to read/write for a person as 2 well:

config.php:

return array(
 'var1'=> 'value1',
 'var2'=> 'value2',
);

test.php:

$config = include 'config.php';
$config['var2']= 'value3';
file_put_contents('config.php', '<?php return ' . var_export($config, true) . ';');

Updated config.php 1 now contains the following:

return array(
 'var1'=> 'value1',
 'var2'=> 'value3',
);
Score: 2

You should use serialize as opposed to json_encode:

http://docs.php.net/serialize

0

Score: 2

The way I store configuration is to put 6 some variables in an external .php file 5 and then when I want to use those files, I 4 say:

<?php include("fileName"); ?>

And that would allow you to share configuration 3 information across many pages. I am not, however, sure 2 that this is the most efficient method, but 1 it seemed to be the easiest to me.

Score: 2

Whilst it's most likely overkill for what 12 you're after, but what I tend to do is store 11 the config data in a database using a PHP 10 class to control the changes to the name/value 9 pairs within. (i.e.: There's no direct DB 8 access from outside this class.)

When a call 7 is made to the PHP config class to change 6 a value, this then writes out a standard 5 PHP include file with all of various values 4 defined on it.

As such, there's no load time 3 performance hit when reading the config 2 data and all config data can be changed 1 within the database via a CMS module.

Score: 0

I am pretty sure you are correct about the 4 int/string values and yes JSON is a way 3 but serialize and unserializing a string 2 will be faster for speed optimization:

This 1 link will help you:

http://docs.php.net/serialize

Score: 0

I use a database with a table called config 6 or settings. The table has two columns:

name [varchar(255)]
value [varchar(255)]

This 5 allows the easy storage of int's, float's, and 4 short strings.

Then I create two functions, GetSetting 3 and SetSetting. These store a row and retrieve 2 a row, respectively. I find that this simplifies 1 the storing of values tremendously.

Score: 0

If you use require instead of include for 5 the config file you will gain a little bit 4 of performance. You can do this if you know 3 that the config file will always be in place, or 2 when you have your own mechanism for checking 1 if it exists.

More Related questions