最新亚洲精品福利在线,欧美一区二区三区大片,久久91无码一区二区三区,色哟哟免费观看视频入口,美女裸露双奶头屁股无裸体

存取json格式的字符串到指定的文件中

時間:2018-04-01 23:07:11 類型:PHP
字號:    

在PHP應用中,經(jīng)常會用到將指定的內容直接存儲到文件中[少而快],而非數(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文件的內容
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;