php创建文件并写入php数组信息

时间:2017-05-06 22:30:16 类型:PHP
字号:    

在php中, 我们一般都是把数组存储在数据库中,  但有时也会对不经常更新的数据存储到文件中, 这样在读取信息时会比从数据库中更快,  实例方法如下:


$myfile = fopen("./tmp/token.php", "w"); 
//文件存在,就打开,不存在就在当前目录的tmp目录下创建token.php件
$txt = "<?php\n";
$txt .= "	return array(\n";
$txt .= "          'names'=>'庄子',\n" ;
$txt .= "          'sex'  =>'男',  \n" ;
$txt .= "          'age'  => 18   \n" ;
$txt .= "	);\n";
$txt .= "?>\n";
fwrite($myfile, $txt); 
//将 $txt 内容写入到 token.php文件
fclose($myfile);
生成的结果如下:



return array(
          'names'=>'庄子',
          'sex'  =>'男',  
          'age'  => 18   
	);