最快速的 PHP 輕鬆可編輯的配置資料儲存方式?
與 JSON 相比,序列化更適合儲存 PHP 變數。
var_export 可用於儲存配置檔案,'include' 可用於載入配置檔案資訊。
以下是透過程式設計方式儲存配置資料且更易讀寫的簡單方法 −
config.php
return array( 'var_1'=> 'value_1', 'var_2'=> 'value_2', );
test.php
$config = include 'config.php'; $config['var_2']= 'value_3'; file_put_contents('config.php', '<?php return ' . var_export($config, true) . ';');
除上述 test.php之外,還可使用以下程式碼 −
$config = include 'config.php'; $config['var_2']= 'value_3'; file_put_contents('config.php', '$config = ' . var_export($config));
更新後的 config.php 包含以下程式碼 −
return array( 'var_1'=> 'value_1', 'var_2'=> 'value_3', );
廣告