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:
So both of the methods looks too comlicated. Is there any better solution?
The solution depends on the task:
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).
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).
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.