0

Could you please clarify, PHP interpreter compiles to bytecode every command and then executes or it first reads all commands and then compiles and executes them?

Yang
  • 8,580
  • 8
  • 33
  • 58
Molarro
  • 1,005
  • 1
  • 10
  • 24
  • The second is the case, but on a per file base. – arkascha Apr 09 '14 at 11:35
  • Bytecode CANNOT be compiled with intuitive methodology (as in variables declared and understood etc). The only way that code can be compiled is in the way it is written - then it is attempted to be executed using the inputs and processes as it is written, from start to finish in the way the logic flow is written. – Fluffeh Apr 09 '14 at 11:36
  • If you're using an opcode cache, then this is only done once per file (unless you actually change that file and force a refresh to the opcode cache) - See http://www.slideshare.net/jpauli/yoopee-cache-op-cache-internals – Mark Baker Apr 09 '14 at 11:38

1 Answers1

0

The short answer is that PHP needs to parse the whole file in order to execute it cleanly, without syntactic errors in it.

An opcode cache (if installed as a Zend extension), can cache the opcodes by replacing the original function to compile a file and execute it only when necessary.

Start exploring from http://lxr.php.net/xref/PHP_5_6/Zend/zend_compile.h line 675 if you would like to know the whole story.

The reason it's all or nothing it's because if a script has syntactic errors and it's already been halfway executed, then it's impossible to undo any damage it may have caused (it's not possible to undo a network communication for instance).

The reason it's on a per-file basis though (and not all or nothing) is for optimisations, since includes and requires can be conditional, not to mention autoloading.

Flavius
  • 13,566
  • 13
  • 80
  • 126