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 WPF使用Canvas绘制矢量图问题
  • ¥15 用三极管设计一个单管共射放大电路
  • ¥15 孟德尔随机化r语言运行问题
  • ¥15 pyinstaller编译的时候出现No module named 'imp'
  • ¥15 nirs_kit中打码怎么看(打码文件是csv格式)
  • ¥15 怎么把多于硬盘空间放到根目录下
  • ¥15 Matlab问题解答有两个问题
  • ¥15 LCD12864中文显示
  • ¥15 在使用CH341SER.EXE时不小心把所有驱动文件删除了怎么解决
  • ¥15 gsoap生成onvif框架