Tower
Tower

Reputation: 102785

Fastest way to store easily editable config data in PHP?

What is the fastest way to store config data in PHP so that it is easily changeable (via PHP)? First I thought about having config.php file, but I can't edit it on fly with PHP, at least not very simply? Then I thought about having XML files, but parsing them for each HTTP request is overwhelming. So, I thought about INI files, but then I figured that INI files are restricted to int/string values. In the end, I have come to the conclusion that JSON encoded file is the best:

$config['database']['host'] = ...;
$config['another']['something'] = ...;
...
json_encode($config);

Since JSON can store arrays, I can create quite complex configurations with it, and it parses faster than INI files.

My question: did I miss something or is there a better way to do this?

Upvotes: 26

Views: 18688

Answers (7)

leepowers
leepowers

Reputation: 38308

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

I like to use var_export for saving config file, and using include for loading config info. This makes it easy to save config data progmatically AND makes the data easy to read/write for a person as 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 now contains the following data:

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

Upvotes: 43

Martien de Jong
Martien de Jong

Reputation: 11

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

Upvotes: 0

Nathan Osman
Nathan Osman

Reputation: 73195

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

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

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

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

Upvotes: 0

John Parker
John Parker

Reputation: 54445

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

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

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

Upvotes: 2

ninu
ninu

Reputation: 920

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

This link will help you:

http://docs.php.net/serialize

Upvotes: 0

zmbush
zmbush

Reputation: 2810

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

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

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

Upvotes: 2

Inspire
Inspire

Reputation: 2060

You should use serialize as opposed to json_encode:

http://docs.php.net/serialize

Upvotes: 2

Related Questions