I know PHP is mostly an interpreted language. Does the PHP interpreter (php.exe in Windows and php file in Linux) do interpretation every time my script executes or only when I change the source? To put it another way, does the PHP interpreter cache interpreted scripts or not?
-
PHP has no cache by default. See [APC](http://php.net/book.apc.php). I think this has been asked already and answered lengthier, feel free to use the search. – hakre Jun 09 '12 at 17:55
-
I searched but I didn't get my answer. some body told it is interpreted language but not like BASIC. I really wanna know how php works deeply – IVIR3zaM Jun 09 '12 at 17:58
-
1You write PHP code. PHP takes it, compiles it to bytecode (opcodes) and then executes the bytecode (opcodes). That's it. PHP does not cache anything when doing that. Enable APC if you would like to cache the bytecode (opcodes), then you don't have that overhead everytime. – hakre Jun 09 '12 at 18:24
-
1@Mat The question is, Are capital letters in the middle of a sentence acceptable grammar? And the answer is Yes. (It's okay without them too, I'm just sayin'.) – TRiG Nov 01 '12 at 11:11
-
@TRiG: after a colon, possible. After a semi-colon, maybe. After a coma, I don't think so. – Mat Nov 01 '12 at 11:22
-
@Mat. After an introductory clause such as *I say to you*, *I tell you*, *I ask*, or *The question is*, if what follows forms a complete sentence, it can start with a capital letter. Arguably, it should start with a capital letter, as this makes the actual statement or question stand out better. – TRiG Nov 01 '12 at 11:35
3 Answers
Yes you have a performance penalty as PHP does interpretation every time. Though, if you have APC(Alternative PHP Cache: http://php.net/apc) installed and configured it will keep whole byte code in memory and will re-build it when some changes occur.

- 16,625
- 3
- 33
- 29
-
I did take a look at APC. it have some functions, that means not work for all over y script only variables can go too cache (absolutely in bytecode) this is nice but not great. however my original script can't go to cache, only rendered class objects. can it help for speed up running time? I know if I use it much, waste my memory space. I read somewhere that facebook hiphop project compile php scripts and can speed up running time 50%. how that work? – IVIR3zaM Jun 09 '12 at 18:12
-
2These functions are for different purposes. APC caches byte code without your involvement, check the configuration manual: http://www.php.net/manual/en/apc.configuration.php – ioseb Jun 09 '12 at 18:13
-
thanks. so APC use RAM Memory to cache opcode files. write? is this job waste Memory space? for a shared host server that maybe disaster! – IVIR3zaM Jun 09 '12 at 18:26
-
@IVIR3zaM : it is the whole point of APC to use ram to save CPU cycles. APC must have a lot of ram to be efficient (GBs on shared servers). The ram used to cache opcode, won't be required to cache disk access to the file. Also opcode is bigger than source code. – bokan Aug 29 '12 at 14:41
This is in essence what happens every time a request arrives:
- PHP reads the file
- PHP compiles the file to a language it can process, the so called opcode
- PHP runs the opcode
There is some overhead in compiling the file into opcode as many have already pointed out, and PHP by default has no cache, so it will do the "compilation" process every time a request arrives even if the file didn't change.
There are some optional modules that can produce opcode caches to avoid that overhead, of which generally the most recommended is APC, since it will ship by default on PHP 6.

- 16,261
- 16
- 62
- 78
-
APC use RAM Memory for cached opcode files? or somewhere in hard disk? or this can be selected by php.ini? – IVIR3zaM Jun 09 '12 at 18:28
-
1
-
thanks.I saw it have some functions for write opcode data in files, can I write these files in some cached directory and use them until original files change and config APC to don't do cache byte codes in RAM and only let me have my cache system? – IVIR3zaM Jun 09 '12 at 18:41
-
1Not really, but writing to the hard disk is slow anyway, you would barely get any gain if you were to store the cache that way. – Mahn Jun 09 '12 at 18:54
-
1Something you can do however is set the maximum memory APC may use. Also, even if your host has a limited amount of memory you may not need more than 20-30mb of RAM to cache all your scripts. – Mahn Jun 09 '12 at 18:57
Yes.
Being an interpreted language, you do pay a performance penalty. However there is some research in the direction of compiling and using it.
Take a look at PHP Accelerator.
Most PHP accelerators work by caching the compiled bytecode of PHP scripts to avoid the overhead of parsing and compiling source code on each request (some or even most of which may never be executed). To further improve performance, the cached code is stored in shared memory and directly executed from there, minimizing the amount of slow disk reads and memory copying at runtime.

- 17,584
- 2
- 43
- 46