note 24685 deleted from function.var-export by sniper

From: Date: Sun, 28 Sep 2003 22:40:54 +0000
Subject: note 24685 deleted from function.var-export by sniper
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-57238@lists.php.net to get a copy of this message
Note Submitter: egbert@NOSPMAteeselink.demon.nl ---- 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.

« previous php.notes (#57238) next »