PHP 是编译还是解释?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1514676/
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 02:53:56  来源:igfitidea点击:

Is PHP compiled or interpreted?

php

提问by nicky

Is PHP compiled or interpreted?

PHP 是编译还是解释?

采纳答案by Thibault Martin-Lagardette

PHP is an interpreted language. The binary that lets you interpret PHP is compiled, but what you write is interpreted.

PHP 是一种解释型语言。允许您解释 PHP 的二进制文件是编译的,但您编写的内容是解释的。

You can see more on the Wikipedia page for Interpreted languages

你可以在维基百科页面上看到更多关于解释语言的信息

回答by Barry Brown

Both. PHP is compiled down to an intermediate bytecode that is then interpreted by the runtime engine.

两个都。PHP 被编译成一个中间字节码,然后由运行时引擎解释。

The PHP compiler's job is to parse your PHP code and convert it into a form suitable for the runtime engine. Among its tasks:

PHP 编译器的工作是解析您的 PHP 代码并将其转换为适合运行时引擎的形式。它的任务包括:

  • Ignore comments
  • Resolve variables, function names, and so forth and create the symbol table
  • Construct the abstract syntax tree of your program
  • Write the bytecode
  • 忽略评论
  • 解析变量、函数名等并创建符号表
  • 构建程序的抽象语法树
  • 编写字节码

Depending on your PHP setup, this step is typically done just once, the first time the script is called. The compiler output is cached to speed up access on subsequent uses. If the script is modified, however, the compilation step is done again.

根据您的 PHP 设置,此步骤通常只执行一次,即第一次调用脚本时。编译器输出被缓存以加快后续使用的访问速度。但是,如果修改了脚本,则会再次执行编译步骤。

The runtime engine walks the AST and bytecode when the script is called. The symbol table is used to store the values of variables and provide the bytecode addresses for functions.

运行时引擎在调用脚本时遍历 AST 和字节码。符号表用于存储变量的值并为函数提供字节码地址。

This process of compiling to bytecode and interpreting it at runtime is typical for languages that run on some kind of virtual runtime machine including Perl, Java, Ruby, Smalltalk, and others.

这种编译为字节码并在运行时解释它的过程对于在某种虚拟运行时机器上运行的语言来说是典型的,包括 Perl、Java、Ruby、Smalltalk 等。

回答by Max

In generally it is interpreted, but some time can use it as compiled and it is really increases performance. Open source tool to perform this operation: hhvm.com

通常它是被解释的,但有些时候可以将它用作编译的,它确实提高了性能。执行此操作的开源工具:hhvm.com

回答by code_burgar

PHP is an interpreted language. It can be compiled to bytecode by third party-tools, though.

PHP 是一种解释型语言。不过,它可以由第三方工具编译为字节码。

回答by Gaurang Deshpande

A compiled codecan be executed directly by the computer's CPU. That is, the executable code is specified in the CPU's nativelanguage

一个编译后的代码可以直接通过电脑的CPU来执行。也就是说,可执行代码是用 CPU 的本机语言指定的

The code of interpreted languagesmust be translated at run-time from any format to CPU machine instructions. This translation is done by an interpreter.

解释语言的代码必须在运行时从任何格式转换为 CPU 机器指令。该翻译由口译员完成。

It would not be proper to say that a language is interpreted or compiled because interpretation and compilation are both properties of the implementationof that particular language, and not a property of the languageitself. So,any language can be compiled or interpreted – it just depends on what the particular implementation that you are using does.

说一种语言被解释或编译是不恰当的,因为解释和编译都是该特定语言的实现属性,而不是语言本身的属性。因此,任何语言都可以编译或解释——这取决于您使用的特定实现的作用。

The most widely used PHP implementation is powered by the Zend Engineand known simply as PHP.The Zend Engine compiles PHP source into a format that it can execute, thus the Zend engineworks as an interpreter.

最广泛使用的 PHP 实现由Zend 引擎提供支持,简称为 PHP。Zend 引擎将 PHP 源代码编译成它可以执行的格式,因此Zend 引擎充当解释器

回答by jrockway

This is a meaningless question. PHP uses yacc (bison), just like GCC. yacc is a "compiler compiler". The output of yacc is a compiler. The output of a compiler is "compiled". PHP is parsed by the output of yacc. So it is, by definition, compiled.

这是一个毫无意义的问题。PHP 使用 yacc (bison),就像 GCC 一样。yacc 是一个“编译器编译器”。yacc 的输出是一个编译器。编译器的输出是“编译”的。PHP 由 yacc 的输出解析。因此,根据定义,它是编译的。

If that doesn't satisfy, consider the following. Both php (the binary) and gcc read your source code and produce an abstract syntax tree. Under versions 4 and 5, php then walks the tree to translate the program to bytecode (the compilation step). You can see the bytecode translated to opcodes (which are analogous to assembly) using the Vulcan Logic Dumper. Finally, php (in particular, the Zend engine) interprets the bytecode. gcc, in comparison, walks the tree and outputs assembly; it can also run assemblers and linkers to finish the process. Calling a program handled by one "interpreted" and another program handled by the other "compiled" is meaningless. After all, programs are both run through a "compiler" with both.

如果这不满足,请考虑以下内容。php(二进制文件)和 gcc 都会读取您的源代码并生成抽象语法树。在版本 4 和 5 下,php 然后遍历树将程序转换为字节码(编译步骤)。您可以看到使用Vulcan Logic Dumper将字节码转换为操作码(类似于汇编)。最后,php(特别是 Zend 引擎)解释字节码。相比之下,gcc 遍历树并输出程序集;它还可以运行汇编器和链接器来完成该过程。调用由一个“解释”处理的程序和另一个“编译”处理的程序是没有意义的。毕竟,程序都是通过“编译器”运行的。

You should actually ask the question you want to ask instead. ("Do I pay a performance penalty as PHP recompiles my source code for every request?", etc.)

你实际上应该问你想问的问题。(“当 PHP 为每个请求重新编译我的源代码时,我是否需要支付性能损失?”等)

回答by Claudiu Creanga

I know this question is old but it's linked all over the place and I think all answers here are incorrect (maybe because they're old).

我知道这个问题很旧,但它到处都有联系,我认为这里的所有答案都不正确(可能是因为它们很旧)。

There is NOsuch thing as an interpreted language or a compiled language. Any programming language can be interpreted and/or compiled.

NO作为一种解释语言或编译语言这样的事情。任何编程语言都可以被解释和/或编译。

First of all a language is just a set of rules, so when we are talking about compilation we refer to specific implementationsof that language.

首先,语言只是一组规则,所以当我们谈论编译时,我们指的是该语言的特定实现

HHVM, for example, is an implementation of PHP. It uses JIT compilation to transform the code to intermediate HipHop bytecode and then translated into machine code. Is it enough to say it is compiled? Some Java implementations (not all) also use JIT. Google's V8 also uses JIT.

例如,HHVM是 PHP 的一个实现。它使用 JIT 编译将代码转换为中间 HipHop 字节码,然后转换为机器码。说编译就够了?一些 Java 实现(不是全部)也使用 JIT。Google 的 V8 也使用 JIT。

Using the old definitions of compiled vs. interpreted does not make sense nowadays.

现在使用编译与解释的旧定义已经没有意义了。

"Is PHP compiled?" is a non-sensical question given that there are no longer clear and agreed delimiters between what is a compiled language vs an interpreted one.

“PHP编译了吗?” 这是一个毫无意义的问题,因为在编译语言与解释语言之间不再有明确且一致的定界符。

One possible way to delimit them is (I don't find any meaning in this dichotomy):

分隔它们的一种可能方法是(我在这种二分法中找不到任何意义):

compiled languagesuse Ahead of Time compilation (C, C++);

编译语言使用 Ahead of Time 编译(C、C++);

interpreted languagesuse Just in Time compilation or no compilation at all (Python, Ruby, PHP, Java).

解释型语言使用即时编译或根本不编译(Python、Ruby、PHP、Java)。

回答by Magnus Andersson

At least it doesn't compile (or should I say optimize) the code as much as one might want it.

至少它不会像人们想要的那样编​​译(或者我应该说优化)代码。

This code...

这段代码...

for($i=0;$i<100000000;$i++);
echo $i;

...delays the program equally much each time it is run.

...每次运行时都同样延迟程序。

It could have detected that it is a calculation that only needs to be done the first time.

它可能已经检测到这是一个只需要第一次完成的计算。

回答by RichieHH

The accepted answer is blatantly false. PHP IS compiled. End of story. Maybe not to native instructions but to an interpreted bytecode.

接受的答案是公然错误的。PHP 已编译。故事结局。也许不是本机指令,而是解释的字节码。