The question is quite simple: When it comes to touching the disk, are these two examples equal, or does scenario #2 touch the disk twice?
include '/path/to/file.php';
if (file_exists('/path/to/file.php'))
include '/path/to/file.php';
I know that scenario #1 touches the disk once. Now, as I understand it file_exists()
caches the path and whether or not the file exists. In order to clear that cache you need to call clearstatcache()
.
But does include
, et alii, also use that cache? Or is it exclusive to file_exists()
?
Both of these examples touch the disk twice – reading the directory and then reading the file. In the first example, this both happens during one command, the second command splits them. It’s very unlikely that the include() will read the directory again, as your OS should contain some sort of HD cache, that should last at least this long.
But you are obviously trying to over-optimize something. Unless you are going to this >100 times in your script, there will not be any performance-difference whatsoever between your two options.