Which is faster: include or file_get_content?

Closed due to the fact that off-topic participants xaja , GrayHoax , Cyrus , PashaPash , Vladimir Glinskikh 16 Oct '15 at 12:39 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • " Questionnaires are forbidden on Stack Overflow in Russian . To get an answer, rephrase your question so that it can be given an unambiguously correct answer." - xaja, GrayHoax, Vladimir Glinskikh
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • readfile will not work. I just need to read the file and that's it. So what is faster ??? - n1k
  • include of course - johniek_comp
  • Vlad, you still have time, but the killer has already told me that it is necessary to include include to connect the php code and file_get_contents to read plain text. - n1k
  • one
    Too different purpose to compare them for speed . - artoodetoo

2 answers 2

  • include performs a bunch of different actions besides simply reading the file, so if you do not need to evaluate for the contents of the file, then include nothing.

  • file_get_contents reads the contents into memory, which may become quite critical for itself. For targeted output to some output it is recommended to use readfile() .

  • I was just told that file_get_contents is loading the server. I now want to know what is faster - n1k
  • 2
    @Deert - If you are interested in performance for your particular case - take and measure. - I described the theoretical difference between file_get_contents, readfile and include , in more detail and with benchmarks - go to the links provided (according to their benchmark, it turns out that the difference is not significant). - The only fundamental difference between file_get_contents and readfile , apparently, in the use of memory. Whether it is critical for you or not, I naturally cannot decide. - Do not suffer premature optimization . If the performance ceases to arrange, then you can already think. - Costantino Rupert
  • @ Kotik_hochet_kushat, include () does not "read the file", but stupidly substitutes the contents of the "inclusive" file in the place where the call is called. --- file_get_contents () also differs from include () in that it does not allow reading PHP code (cap =)), for example. --- And by the way, what kind of "file evaluation" are we talking about? - Salivan
  • one
    @Asen - It's good that you know how to use a Google translator :) - [[Documentation on include ]] [1] “Of course, it is clear that include equivalent to eval(get_file_contents(...)) with an accuracy of the caching mechanism used, but depending on the behavior of eval behavior of the include will change. [1]: php.net/manual/en/function.include.php - Costantino Rupert
  • @ Kotik_hokhet_kusat, and you, as I understand it, are friends with him? =) - Salivan

Answering the question if the file is located locally - then, of course, file_get_contents will be executed faster :)

True, I omit the fact that the result of the work will not be identical :)

  • Thank you all decided to use file_get_contents () - n1k