在PHP應(yīng)用中,經(jīng)常會用到將指定的內(nèi)容直接存儲到文件中[少而快],而非數(shù)據(jù)庫中[麻煩慢],比如我們微信開發(fā)經(jīng)常讀取的access_token, 程序員使用的一些基本配置文件等,這里給大家介紹下具體的使用方法
//將json格式的字符串存到指定的php文件中
function set_php_file($filename, $content)
{
$fp = fopen($filename, "w");
fwrite($fp, "<?php exit();?>" . $content);
fclose($fp);
}
//獲取php文件的內(nèi)容
function get_php_file($filename)
{
return trim(substr(file_get_contents($filename), 15));
}
$filename = "./a.php";
$data = ["names"=>"莊子","age"=>18,"hobby"=>"NBA,足球,乒乓,象棋"];
$content = json_encode($data);
set_php_file($filename,$content);
$rs = json_decode(get_php_file($filename));
echo $rs->names;
