Fastest way to store easily editable config data in PHP?

后端 未结 7 1393
借酒劲吻你
借酒劲吻你 2020-12-01 03:46

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

7条回答
  •  一个人的身影
    2020-12-01 04:30

    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', '

    Updated config.php now contains the following:

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

提交回复
热议问题