PHP脚本究竟是如何执行的?

时间:2021-11-14 20:45:41

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.

我只是在想自己“PHP脚本究竟是如何执行的?”我认为它首先被解析为语法错误等,然后解释和执行。

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?

那么,PHP文件究竟是如何被解释和执行的呢?这涉及哪些阶段?包含的文件如何适合脚本的解析?

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

这只是为了帮助我理解它。我很感兴趣,无法与谷歌找到一个好的答案。

3 个解决方案

#1


15  

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

基本上,每次加载PHP脚本时,都需要两个步骤:

  • The PHP source code is parsed, and converted to what's called opcodes
    • Kind of an equivalent of JAVA's bytecode
    • 类似于JAVA的字节码
    • If you want to see what those look like, you can use the VLD extension
    • 如果您想查看它们的外观,可以使用VLD扩展
  • PHP源代码被解析,并转换为所谓的操作码相当于JAVA的字节码类型如果你想看看它们是什么样的,你可以使用VLD扩展
  • Then, those opcode are executed
  • 然后,执行那些操作码

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

这些来自Sebastian Bergmann的幻灯片可以帮助您更好地理解这个过程:PHP Compiler Internals

#2


15  

PHP is a compiled language since PHP 4.0

PHP是PHP 4.0以来的编译语言

The idea of what is a compiler seems to be a subject that causes great confusion. Some people assume that a compiler is a program that converts source code in one language into an executable program. The definition of what is a compiler is actually broader than that.

什么是编译器的想法似乎是一个引起很大混乱的主题。有些人认为编译器是一种将一种语言的源代码转换为可执行程序的程序。什么是编译器的定义实际上比那更广泛。

A compiler is a program that transforms source code into another representation of the code. The target representation is often machine code, but it may as well be source code in another language or even in the same language.

编译器是将源代码转换为代码的另一种表示的程序。目标表示通常是机器代码,但它也可以是另一种语言的源代码,甚至是同一种语言。

PHP became a compiled language in the year 2000, when PHP 4 was released for the first time. Until version 3, PHP source code was parsed and executed right away by the PHP interpreter.

PHP在2000年首次发布时成为了一种编译语言。在版本3之前,PHP解释器立即解析并执行了PHP源代码。

PHP 4 introduced the the Zend engine. This engine splits the processing of PHP code into several phases. The first phase parses PHP source code and generates a binary representation of the PHP code known as Zend opcodes. Opcodes are sets of instructions similar to Java bytecodes. These opcodes are stored in memory. The second phase of Zend engine processing consists in executing the generated opcodes.

PHP 4引入了Zend引擎。该引擎将PHP代码的处理分为几个阶段。第一阶段解析PHP源代码并生成称为Zend操作码的PHP代码的二进制表示。操作码是类似于Java字节码的指令集。这些操作码存储在内存中。 Zend引擎处理的第二阶段包括执行生成的操作码。

Form more information go to http://www.phpclasses.org/blog/post/117-PHP-compiler-performance.html

有关更多信息,请访问http://www.phpclasses.org/blog/post/117-PHP-compiler-performance.html

#3


2  

Here is also a list of all the parser tokens.

这里还列出了所有解析器令牌。

#1


15  

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

基本上,每次加载PHP脚本时,都需要两个步骤:

  • The PHP source code is parsed, and converted to what's called opcodes
    • Kind of an equivalent of JAVA's bytecode
    • 类似于JAVA的字节码
    • If you want to see what those look like, you can use the VLD extension
    • 如果您想查看它们的外观,可以使用VLD扩展
  • PHP源代码被解析,并转换为所谓的操作码相当于JAVA的字节码类型如果你想看看它们是什么样的,你可以使用VLD扩展
  • Then, those opcode are executed
  • 然后,执行那些操作码

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

这些来自Sebastian Bergmann的幻灯片可以帮助您更好地理解这个过程:PHP Compiler Internals

#2


15  

PHP is a compiled language since PHP 4.0

PHP是PHP 4.0以来的编译语言

The idea of what is a compiler seems to be a subject that causes great confusion. Some people assume that a compiler is a program that converts source code in one language into an executable program. The definition of what is a compiler is actually broader than that.

什么是编译器的想法似乎是一个引起很大混乱的主题。有些人认为编译器是一种将一种语言的源代码转换为可执行程序的程序。什么是编译器的定义实际上比那更广泛。

A compiler is a program that transforms source code into another representation of the code. The target representation is often machine code, but it may as well be source code in another language or even in the same language.

编译器是将源代码转换为代码的另一种表示的程序。目标表示通常是机器代码,但它也可以是另一种语言的源代码,甚至是同一种语言。

PHP became a compiled language in the year 2000, when PHP 4 was released for the first time. Until version 3, PHP source code was parsed and executed right away by the PHP interpreter.

PHP在2000年首次发布时成为了一种编译语言。在版本3之前,PHP解释器立即解析并执行了PHP源代码。

PHP 4 introduced the the Zend engine. This engine splits the processing of PHP code into several phases. The first phase parses PHP source code and generates a binary representation of the PHP code known as Zend opcodes. Opcodes are sets of instructions similar to Java bytecodes. These opcodes are stored in memory. The second phase of Zend engine processing consists in executing the generated opcodes.

PHP 4引入了Zend引擎。该引擎将PHP代码的处理分为几个阶段。第一阶段解析PHP源代码并生成称为Zend操作码的PHP代码的二进制表示。操作码是类似于Java字节码的指令集。这些操作码存储在内存中。 Zend引擎处理的第二阶段包括执行生成的操作码。

Form more information go to http://www.phpclasses.org/blog/post/117-PHP-compiler-performance.html

有关更多信息,请访问http://www.phpclasses.org/blog/post/117-PHP-compiler-performance.html

#3


2  

Here is also a list of all the parser tokens.

这里还列出了所有解析器令牌。