Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Fastest way to store easily editable config data in PHP?
In PHP, the fastest and most efficient way to store easily editable config data is using var_export() with native PHP arrays. This approach offers better performance than JSON serialization and provides human-readable configuration files.
Why Use var_export() for Config Data?
The var_export() function generates valid PHP code that can be directly included, making it faster than parsing JSON or other formats. Combined with PHP's include statement, this creates an optimal configuration system.
Creating the Config File
First, create a basic configuration file that returns an array −
config.php
<?php
return array(
'var_1' => 'value_1',
'var_2' => 'value_2',
);
Reading and Updating Config Data
Load the configuration and update values programmatically −
test.php
<?php
$config = include 'config.php';
$config['var_2'] = 'value_3';
file_put_contents('config.php', '<?php return ' . var_export($config, true) . ';');
Alternative Approach
You can also store the config as a variable instead of returning it −
<?php
$config = include 'config.php';
$config['var_2'] = 'value_3';
file_put_contents('config.php', '<?php $config = ' . var_export($config, true) . ';');
Updated Config File Result
After running the update script, the config.php file will contain −
<?php
return array(
'var_1' => 'value_1',
'var_2' => 'value_3',
);
Conclusion
Using var_export() with PHP arrays provides the fastest method for storing editable config data. It's more efficient than JSON serialization and creates human-readable files that can be easily modified both programmatically and manually.
