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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?