How to write an array to a file in PHP _php tutorial

Source: Internet
Author: User
In PHP we provide a function var_export he can directly put the PHP code into a file OH.
The code is as follows Copy Code

Var_export ($times, true); Cannot write to file after not added
$fp = fopen (' aa.txt ', ' w+ ');
Fwrite ($fp, Var_export ($times, true));
Fclose ($FP);

Method Two

Using Serializ and Unserialize functions

The code is as follows Copy Code

if (Isset ($_post[' Sub ')) {
$cfg = Array (' Contact ' =>$_post[' contact '); Storing the data in an array
File_put_contents ('./data/contact.cache ', serialize ($cfg));
After serializing the array, write to the Contact.cache,
$this->redirect (' other/contact ');//Jump
}
else{
$fp = fopen ('./data/contact.cache ', ' r ');//Read
$CF = Unserialize (Fread ($fp, FileSize ('./data/contact.cache '));//deserialization, and assignment
$this->assign (' cfg ', $CF);//Send to foreground template
$this->display (' other/contact ');
}

http://www.bkjia.com/PHPjc/629652.html www.bkjia.com true http://www.bkjia.com/PHPjc/629652.html techarticle in PHP we provide a function var_export he can directly put the PHP code into a file OH. The code below copies the code Var_export ($times, true);

  • 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.