views:

82

answers:

3

I was just thinking to myself "How exactly is a PHP script executed?" I thought it was parsed first for syntax errors etc, and then interpreted and executed.

However, I don't know why I believe that is correct. I'm probably wrong.

So, how exactly is a PHP file interpreted and executed? What stages does this involve? How do included files fit into the parsing of the script?

This is just to help me get my head around it. I'm interested and can not find a good answer with Google.

Thanks!

+1  A: 

Basically, each time a PHP script is loaded, it goes by two steps :

  • The PHP source code is parsed, and converted to what's called opcodes
    • Kind of an equivalent of JAVA's bytecode
    • If you want to see what those look like, you can use the VLD extension
  • Then, those opcode are executed

These slides from Sebastian Bergmann, on slideshare, might help you understand that process a bit better : PHP Compiler Internals

Pascal MARTIN
Thanks, that looks interesting. +1
alex
+3  A: 

Take a listen to:
http://techportal.ibuildings.com/2010/02/02/php-compiler-internals/

This is a great question, I wonder the same thing.

In this presentation we introduce a new language construct to demonstrate how one might go about modifying the PHP interpreter. The internals of which follow a pattern common to many language implementations, with lexical analysis, parsing, code generation, and execution phases.

(As a side note: ibuildings seems to be a leader in PHP development. They are based in the UK. Who is a similar company in the US?)

Christopher Altman
Thanks for the link +1
alex
A: 

Here is also a list of all the parser tokens.

alex