PHP writes an array to the file instance code _php tutorial

Source: Internet
Author: User
There are many ways to write an array to a file in PHP, like when we want to make a cache file, we convert the PHP array into a file and then save it to a. php file, which is called directly when called.

PHP writes an array to a file, which is implemented by sequence and inverse sequence. Please look at the code


$file = "./data/file.cache";
File_put_contents ($file, serialize ($array));//write Cache

The code is as follows Copy Code

$file = "./data/file.cache";

$array = Array ("Count" = "3000",

"num" = "300");

Cache

File_put_contents ($file, serialize ($array));//write Cache
?>

$file = "./data/file.cache";
$handle = fopen ($file, "R");
$cacheArray = Unserialize (Fread ($handle, FileSize ($file)));
Print_r ($cacheArray);
?>

Write an array of tests to a PHP file:

The code is as follows Copy Code

$write _array = Array (' 1 ' = ' = ' Oneone ', ' 2 ' = ' + ', ' 3 ' = ' three ', ' 4 ' = ' Four ', ' 5 ' = ' five '); String processing
$string _start = " "; $string = $string _start. $string _process. $string _end;//start writing to file
echo file_put_contents (' test_array.php ', $string);
?>


Two functions are used here:

The code is as follows Copy Code

1,var_export ():

var_export-is used to output or return a string representation of a variable, and the difference between it and var_dump () is that var_export () can be used to return the structure information about the variable passed to the function, and its return representation is a valid PHP code if "echo $ string_process; ", you can see the output:

The code is as follows Copy Code
Array (1 = ' Oneone ', 2 = ' both ', 3 = ' three ', 4 = ' four ', 5 = ' five ',)


And it's what we're going to write to the test_array.php file (minus the PHP tag);

The var_dump () function is used to print information about a variable, it is used only to "print", not return a value, its prototype is void Var_dump (...), we come to "var_dump ($string _process);", you can see the output:

The code is as follows Copy Code
String "Array (1 = ' Oneone ', 2 = '", 3 = ' three ', 4 = ' four ', 5 = ' five ',) "

You can see the output of string (86) "..." again, stating that Var_export () returns a string.

http://www.bkjia.com/PHPjc/633068.html www.bkjia.com true http://www.bkjia.com/PHPjc/633068.html techarticle There are many ways to write an array to a file in PHP, like when we want to make a cache file, we convert the PHP array into a file and then save it to a. php file, which is called directly when called. P ...

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