A lot of time we need to save PHP generated PHP code inside the php file, like if the use of the cache when some basic data is saved directly to a specified PHP cache file, let's look at the example below.
The code is as follows |
Copy Code |
Function Cachearr (& $data) { if (! $data) throw new Exception (' array cannot be null '); foreach ($GLOBALS as $key = = $value) { $str = $GLOBALS [$key]; $GLOBALS [$key]= ' changed '; if (& $data = = ' changed ') { $strName = $key; Break } $GLOBALS [$key]= $str; } Ob_clean (); Ob_start (); echo " function Echoarr ($arr, $arrName) { $arrCount =count ($arr); $i ==0; foreach ($arr as $key = = $value) { + + $i; if (Is_array ($value)) { echo "n". (Is_numeric ($key)? $key: ". $key. '). ' =>array ('; Echoarr ($value, $arrName. ( Is_numeric ($key)? ' ['. $key. '] ': ' ['. $key. '] '); if ($i! = $arrCount) echo '), '; else echo ') '; Continue } if ($i! = $arrCount) echo ((Is_numeric ($key))? $key: ". $key. '). ' = '. (Is_numeric ($value)? $value: ". $value."). ', '; else Echo ((Is_numeric ($key))? $key: ". $key. '). ' = '. (Is_numeric ($value)? $value: ". $value."); } } Echo ' $ '. $strName. ' =array ('; Echoarr ($data, "); Echo '); echo "N?>"; $file =fopen ($strName. Arr.php ', ' W '); Fwrite ($file, ob_get_contents ()); Fclose ($file); Ob_clean (); return true; } ?> |
Instance
The code is as follows |
Copy Code |
Storing arrays $hello =array (1=> ' test ', 2=>array (' hello123 ')); Cachearr ($hello); Unset ($hello); Reading an array Require ' hello.arr.php '; Print_r ($hello); ?>
|
http://www.bkjia.com/PHPjc/631651.html www.bkjia.com true http://www.bkjia.com/PHPjc/631651.html techarticle A lot of time we need to save PHP generated PHP code inside the php file, like if the use of the cache when some basic data is saved directly to a specified PHP cache file, the following ...