note 24685 added to function.var-export
| From: | egbert at NOSPMAteeselink dot demon dot nl | Date: | Sun, 25 Aug 2002 18:49:25 +0000 |
| Subject: | note 24685 added to function.var-export | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-35601@lists.php.net to get a copy of this message | ||
combined with the eval() function, this function is very useful for saving small bits of
(tree-structured) data in an easy-to-read format to disk:
function setData($sFile,$aData) { /* var_export, php4.2.0 and up, turns an array into the php code
that would create it. */
$fp = fopen($sFile,"w");
fwrite($fp,var_export ($aData, TRUE));
fclose($fp);
}
function getData($sFile) { /* reverse var_export */
return(eval("return(".join("",file($sFile)).");"));
}
if $aData is an array with a somewhat complex structure, defining a series of database tables for it
and writing all corresponding database queries would be rather much work. of course in cases of
large amounts of data, db's should still be preferred as a db generally searches data faster
than php searches large arrays. also this requires one to load the full array into the php memory,
which would be rather silly if you'd only need relatively little information from a large
database. still this can be a huge timesaver for smaller amounts of data. a disadvantage is also
that a little bug in the code can screw a dataset up a lot, so be sure to backup a bit.
the advantage of using this over the serialize() and unserialize() functions is that the data can be
edited relatively easy with any text editor.
one can also use wddx for a comparable effect, but not all php installs have it enabled.
for possible speedups or ease of management one might still prefer storing the generated texts into
a database field rather than a disk file, of course.
if disk space is important, try gzipping the whole bunch and use serialize() instead.
--
http://www.php.net/manual/en/function.var-export.php
http://master.php.net/manage/user-notes.php?action=edit+24685
http://master.php.net/manage/user-notes.php?action=delete+24685
http://master.php.net/manage/user-notes.php?action=reject+24685