This site requires Flash Player 9 or better.

PHP

Caching an array to disk

I discovered the var_export function in PHP about a year ago. It is able to return a string that represents the passed variable. The returned string is parsable by PHP.

One way to use this function would be to cache an array to a file.

In example:

export.php
<?php
//will write the array to disk
$ar = array('test','testing');
$content = var_export($ar,true);
file_put_contents('cache.php',"<?php\n\$cache=$content;");
?>
 
import.php
<?php
//will output the cached array
include 'cache.php';
var_dump( $cache );

Discussion

One comment for “Caching an array to disk”

  1. An even easier method is writing return $content to the file, in this case you can assign the cached data to a var by using $cache = include 'cache.php'; and avoid naming collisions

    Posted by Chris R. | December 9, 2008, 10:33 am

Post a comment