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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2720488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:29:06  来源:igfitidea点击:

How exactly is a PHP script executed?

phpparsinginterpreter

提问by alex

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 whyI 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.

这只是为了帮助我理解它。我很感兴趣,但在 Google 上找不到好的答案。

回答by Vishnu Saini

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 年首次发布 PHP 4 时成为编译语言。在版本 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 源代码并生成 PHP 代码的二进制表示,称为 Zend 操作码。操作码是类似于 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

回答by Pascal MARTIN

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
    • If you want to see what those look like, you can use the VLD extension
  • Then, those opcode are executed
  • PHP 源代码被解析,并转换为所谓的操作码
    • 相当于 JAVA 的字节码
    • 如果你想看看那些看起来像什么,你可以使用VLD 扩展
  • 然后,这些操作码被执行

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

这些来自 Sebastian Bergmann 在 slideshare 上的幻灯片可能会帮助您更好地理解该过程:PHP Compiler Internals

回答by alex

Here is also a list of all the parser tokens.

这里也是所有解析器标记的列表。