However more often than not, the contents are static, usually html template component. With static includes you have many more options available.
We will analyse some of these functions to find out which one is most suitable when dealing with files with static content. We use the term function loosely, because require and include are not real functions but language constructs.
|string file_get_contents ( string filename [, int use_include_path])||Reads entire file into a string|
|int fpassthru ( resource handle)||Output all remaining data on a file pointer|
|string fgets ( resource handle [, int length])||Gets line from file pointer|
|array file ( string filename [, int use_include_path])||Reads entire file into an array|
|includes and evaluates the specific file.|
|int readfile ( string filename [, int use_include_path])||Outputs a file|
We compensate for file caching and background processes by executing each script 4 times and taking the average (mean) of the result number 2-4. The first result is always rejected. Any result that appears to be outlier is rejected. The mean is rounded to 5 decimal places.
|Function||Sample Usage||Time (s)||Memory (b)|
|fgets||$fp = fopen($filename,"rb");
The only conclusion that can be drawn from these studies is that fpassthru and readfile are equally good if you wish to include static content as part of the script’s output.
Before you rush off to change all your includes and requires into readfiles or fpassthrus let’s run the same test with a smaller (32Kb file). 32Kb is a more realistic size for an included file.
|Function||Time (s)||Memory (b)|
|32Kb File||1Mb File||32Kb File||1Mb File|
The most significant feature of these results is that both fpassthru and readfile scale really well. In other words, memory consumption and execution time does not increase significantly with increase in file size. That does not always mean your script will be faster just because you use these functions instead of require or include.