Home >Backend Development >PHP Tutorial >Save a piece of data to a txt file in PHP and display its content, _PHP Tutorial

Save a piece of data to a txt file in PHP and display its content, _PHP Tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-13 10:20:531003browse

Save a piece of data into a txt file in php and display its content,

The data here can be basic data types, arrays, objects, etc.;

You can use serialize to serialize when storing, but you must first use unserialize to deserialize when retrieving.

<&#63;php
  $data = array("上海","西安","北京");

  //将数组存到指定的text文件中
  file_put_contents("E:/data.txt",serialize($data));
  //获取数据
  $datas = unserialize(file_get_contents("E:/data.txt"));
  print_r($datas);
&#63;>

Of course, you can also use json_encode. The array here can be accessed as key-value pairs, and you need to use json_decode to escape when accessing.

<&#63;php
  $data = array("现代"=>"上海","文化"=>"西安","首都"=>"北京");

  //将数组存到指定的text文件中
  file_put_contents("E:/data.txt",json_encode($data));
  //获取数据
  $datas = json_decode(file_get_contents("E:/data.txt"));
  print_r($datas);
&#63;>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/864477.htmlTechArticleSave a piece of data into a txt file in php and display its content. The data here can be basic data types , arrays, objects, etc.; You can use serialize to sequence when storing...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn