Save PHP Code to PHP file implementation Method _php tutorial

Source: Internet
Author: User
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 ...

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