6

Let's say that I force mod_php to take in .html files. Let's say that there's 0 PHP code in one of these files.

If I browse to that file with APC enabled, what happens? Does it get cached or does it still have to be read from the disk?

The other scenario, I have a .php file. What happens to the content outside PHP tags? Does it get stored in memory? Or does it have to be read from the disk every time?

hakre
  • 193,403
  • 52
  • 435
  • 836
nwalke
  • 3,170
  • 6
  • 35
  • 60

1 Answers1

7

If I browse to that file with APC enabled, what happens? Does it get cached or does it still have to be read from the disk?

If you have .html files set to be parsed as PHP, then yes -- it will be cached. Sort of.

Specifically, PHP will generate an optree for the document, which ends up being a very short and boring program with a single very large string constant in it. This will end up getting stored in memory. HOWEVER, if that's all you're after, you'd be much better off using something like mod_mem_cache (unrelated to memcached!) instead, as it's actually designed to cache static content.

The other scenario, I have a .php file. What happens to the content outside PHP tags? Does it get stored in memory? Or does it have to be read from the disk every time?

As I alluded to earlier, content outside PHP tags is still treated as part of the PHP "program" -- although it's treated a little differently internally, a chunk of static text surrounded by ?> ... <?php (or at the beginning or end of a file) is effectively treated as if it were in an echo "...". (Except without all the gotchas involving escaping within that string.) For instance, the following two code blocks are functionally more or less identical, other than some differences in whitespace:

<?php
  if ($condition) {
      echo "Hello";
  }
?>

vs.

<?php if ($condition) { ?>
    Hello
<?php } ?>
  • I don't want to implement this. Was just curious. – nwalke Oct 18 '12 at 17:17
  • Does the same sort of thing then happen for php files with html outside of the php tags? – nwalke Oct 18 '12 at 17:19
  • 1
    Edited to cover that! Short version: Yes. –  Oct 18 '12 at 17:25
  • Incidentally, this is a good reason to not parse static files (like HTML) as PHP unless you absolutely need to -- running them through the PHP parser slows things down, and will end up blowing more useful opcodes out of your APC cache. –  Oct 18 '12 at 17:28