dongyan5815 2018-05-04 09:11
浏览 24
已采纳

在PHP中运行脚本之间存储变量值的最佳方法是什么?

Let's say I want to have a variable which should keep it's value.

Simple example:

<?php
$files = array('file1.txt','file2.txt','file3.txt'); // list of the files to process
$file = file_get_contents($files[$i]);
process_the_file($file);
$i++;
exit();

So I'd like to process only one file per script run. And in my case the index of the currently processed file is $i.

From my pont of view there are two methods available:

  1. Store variable in a file. I can serialise (and unserialize upon initialization) any data and store in a text file. However, I should keep an eye on the script termination to be sure that the needed values will be stored before the script terminated.
  2. Use database. It looks bulky and as previously described - I should keep an eye on storing.

So both of the methods looks too comlicated. Is there any better solution?

  • 写回答

1条回答 默认 最新

  • doushou3814 2018-05-04 12:57
    关注

    The solution depends on the task:

    1. Use a cache system (memcached, redis, etc.) - when you need a runtime storage (i.e. values may be deleted and will not break an application logic).

    2. Use DB(MySQL, Mongo) - when you need a permanent storage for values (i.e. values may not be deleted - this will break an application logic).

    3. Use Files for both variants.

    Sessions is good enough when you need to store data for one user between requests.

    The best solution is the solution that the most suitable for your task. I would recommend to use either 1 or 2 variant: in projects where I am working we use memcached for a runtime storage and mongodb for a permanent storage.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题