Fastest way to store easily editable config data in PHP?
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:
return array( 'var1'=> 'value1', 'var2'=> 'value3',);
You should use serialize as opposed to json_encode:
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.