Search code examples
phpcachingfile-exists

PHP - file_exists or array cache?


I have a PHP code which includes several files if they exists. It might be 150 files to include.

Is it slow to use file_exists in this case? Should I build an array to create a cache of the file structure instead?

Is there other ways to make some kind of cache of the file structure?


Solution

  • 150 files during serving single request?
    If so, you have to reduce this number, not because of 150 file_exists but because of 150 includes.

    If it depends on the requested parameters and being 3-4 per request, it's OK.

    You cannot use a regular array as a cache though. PHP doesn't preserve variables between requests. Instead of inventing such cache you have tho rethink your application structure.