How to save the php code to the php file

Source: Internet
Author: User
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 saved to the cache, it will be saved to a specified php Cache file. next we will

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, let's take a look at the instance.

  1. Function cacheArr (& $ data ){
  2. If (! $ Data) throw new Exception ('Array cannot be blank ');
  3. Foreach ($ GLOBALS as $ key => $ value ){
  4. $ Str = $ GLOBALS [$ key];
  5. $ GLOBALS [$ key] = 'changed ';
  6. If (& $ data = 'changed '){
  7. $ StrName = $ key;
  8. Break;
  9. }
  10. $ GLOBALS [$ key] = $ str;
  11. }
  12. Ob_clean ();
  13. Ob_start ();
  14. Echo"
  15. Function echoArr ($ arr, $ arrName ){
  16. $ ArrCount = count ($ arr); $ I = 0;
  17. Foreach ($ arr as $ key => $ value ){
  18. ++ $ I;
  19. If (is_array ($ value )){
  20. Echo "n". (is_numeric ($ key )? $ Key: '''. $ key. '''). '=> array (';
  21. EchoArr ($ value, $ arrName. (is_numeric ($ key )? '['. $ Key. ']': '['. $ key. ''] ');
  22. If ($ I! = $ ArrCount) echo '),';
  23. Else echo ')';
  24. Continue;
  25. }
  26. If ($ I! = $ ArrCount) echo (is_numeric ($ key ))? $ Key: '''. $ key. '''). '=>'. (is_numeric ($ value )? $ Value: '''. $ value .''').',';
  27. Else echo (is_numeric ($ key ))? $ Key: '''. $ key. '''). '=>'. (is_numeric ($ value )? $ Value: '''. $ value .''');
  28. }
  29. }
  30. Echo '$'. $ strName. '= array (';
  31. EchoArr ($ data ,'');
  32. Echo ');';
  33. Echo "n?> ";
  34. $ File = fopen ($ strName. '. arr. php', 'w ');
  35. Fwrite ($ file, ob_get_contents ());
  36. Fclose ($ file );
  37. Ob_clean ();
  38. Return true;
  39. }
  40. ?>

The instance code is as follows:

  1. // Storage array
  2. $ Hello = array (1 => 'test', 2 => array ('hello123 '));
  3. CacheArr ($ hello );
  4. Unset ($ hello );
  5. // Read the array
  6. Require 'Hello. arr. php ';
  7. Print_r ($ hello );
  8. ?>

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.