PHP saves a piece of data to a TXT file and displays its contents, _php tutorial

Source: Internet
Author: User

PHP saves a piece of data in a TXT file and displays its contents,


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

It can be serialized with serialize when it is stored, but it should be deserialized with unserialize first when it is taken.

<?php  $data = Array ("Shanghai", "Xian", "Beijing");  Saves the array to the specified text file  file_put_contents ("E:/data.txt", Serialize ($data));  Get data  $datas = Unserialize (file_get_contents ("E:/data.txt"));  Print_r ($datas);? >

Of course, you can also use Json_encode, where the array can be accessed by key-value pairs, which are escaped with Json_decode.

<?php  $data = Array ("modern" = "Shanghai", "culture" = "Xi ' an", "capital" = "Beijing");  Saves the array to the specified text file  file_put_contents ("E:/data.txt", Json_encode ($data));  Get data  $datas = Json_decode (file_get_contents ("E:/data.txt"));  Print_r ($datas);? >



http://www.bkjia.com/PHPjc/864477.html www.bkjia.com true http://www.bkjia.com/PHPjc/864477.html techarticle PHP saves a piece of data in a TXT file and displays its contents, where the data can be a basic data type, an array, an object, etc., and can be serialized with serialize when stored ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.