Save the php code to the php file. For a lot of time, we need to save the php code generated by php to the php file. for example, if some basic data is used to cache, it will be directly saved to a specified php Cache file, we need to save the php code generated by php to the php file for a lot of time below. for example, if some basic data is used in the cache, it will be directly saved to a specified php Cache file, let's take a look at the instance.
The code is as follows: |
|
Function cacheArr (& $ data ){ If (! $ Data) throw new Exception ('Array cannot be blank '); 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: |
|
// Storage array $ Hello = array (1 => 'test', 2 => array ('hello123 ')); CacheArr ($ hello ); Unset ($ hello ); // Read the array Require 'Hello. arr. php '; Print_r ($ hello ); ?>
|
...