Home  >  Article  >  Backend Development  >  What is php used to parse?

What is php used to parse?

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-09-28 14:32:442963browse

What is php used to parse?

PHP is a server scripting language and an interpreted language. It is widely used in the construction of small and medium-sized websites. It is not as heavy as Java and is faster to develop. But how is this scripting language parsed by the server?

We all know that PHP can be embedded in HTML, but the suffix of the file must end with .php. If it is .html, the PHP code snippet will be directly commented by the browser. Here I will No more demonstration, let’s look at an example:

#test1.php
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>test</title>
</head>
<body>
  <?php   
    echo "我是一段PHP脚本";
  ?>
</body>
</html>

The result when accessing through a browser after using PHPstudy is:

What is php used to parse?

This is a paragraph Very simple code. We can see that the server directly passes the parsed result to the browser. In fact, when the server finds that the requested resource suffix is ​​.php, it will call the PHP parser to parse it and execute the PHP code inside. Then respond the resource to the client. So here comes the point, how is PHP code parsed? First of all, we must first understand the related concepts of cgi, fastcgi, and php-fpm.

Related recommendations: "php Getting Started Tutorial"

What is cgi?

cgi (Common Gateway Interface) is actually a protocol. The program that implements the cgi protocol can be called a cgi program. CGI applications can interact with the browser and can also interact with the database API Communicate with external data sources such as database servers and obtain data from the database servers. After formatting into an HTML document, it is sent to the browser, or the data obtained from the browser can be put into the database. And php-cgi is a protocol used to parse PHP code. But this agreement has several drawbacks. First of all, every time the client requests a PHP script file, the server will fork a process and call the parser to parse the script. When the script is executed, the process will be killed, and each fork process will Go through php.ini to initialize the environment. The process cannot be reused, and the IO overhead of php.ini is increased. Limits the execution speed of PHP. So the smart phper came up with a better cgi protocol, which was the fastcgi protocol that came out later.

fastcgi protocol

Gu Ming thought, faster cgi protocol. So where is he soon?

First of all, fastcg solves the problems of the original php-cgi. It allows multiple requests to be processed in one process, instead of directly ending the process after one request is processed. This greatly improves the performance of the web server. improve. In fact, Fastcgi will first start a master, parse the configuration file, initialize the execution environment, and then start multiple workers. When a request comes in, the master passes it to a worker and can immediately accept the next request. This avoids duplication of work and is naturally highly efficient. And when there are not enough workers, the master can pre-start several workers according to the configuration and wait; of course, when there are too many idle workers, some will be stopped, which improves performance and saves resources.

php-fpm

PHP-FPM (FastCGI Process Manager: FastCGI Process Manager) is a PHPFastCGI manager for PHP before 5.3.3 For php, it is a patch package designed to integrate FastCGI process management into the PHP package. If you are using PHP before PHP5.3.3, you must patch it into your PHP source code, and you can use it after compiling and installing PHP. In fact, we can think of php-fpm as an interpreter. We can use the command: ps -aux | grep php-fpm to view the corresponding process status, as shown below:

What is php used to parse?

Using php-fpm can restart smoothly, general modifications After php.ini, the php-cgi process cannot be restarted smoothly, which means you must restart the service to reload the new configuration. The php-fpm processing mechanism for this is that new workers use new configurations, and existing workers can rest after processing the work at hand. This mechanism is used to smooth the transition.

但是传统的php-fpm的worker是同步阻塞的,这在一定程度下也限制了程序的运行速度,并且普通的PHP是无法常驻内存的,也就意味着我们每次执行代码都需要将相同的东西重新加载到新内存去,这点跟java的servlet就不同了,java的servlet在用户访问后实例化,下一个用户就不会再次进行实例化。为了解决这些问题,强大的Rango写出了swoole拓展,swoole和fpmd的进程模型是相同的,manager都是负责管理子进程的创建和回收。但php-fpm的worker进程是同步阻塞的,swoole的worker进程是异步非阻塞的。并且swoole的http-server和fpm的差异是http-server是内存常驻的,PHP程序变成长生命周期的了。变量和对象在使用请求结束后并不会销毁,可以复用。这也就是为什么我们说swoole开启了PHP的新世界。

 我们可以用简单的几句就可以创建一个异步非阻塞的http-server甚至是http2协议的server。例如:

$http = new swoole_http_server("127.0.0.1", 9501);
$http->on(&#39;request&#39;, function ($request, $response) {
    $response->end("<h1>Hello Swoole. #".rand(1000, 9999)."</h1>");
});
$http->start();

swoole的高性能体现在它是一个纯c编写的拓展,并且使用了全内存缓存和异步IO。使得它相对于Node.js默认是单线程的无法利用全部CPU,Golang的协程调度本身有一定性能消耗相比,有着更加不错的性能。

用图解析一波,php-fpm是这样的(图片来源于网络):

What is php used to parse?

而swoole的http-server是这样的(这里的cache应该理解成框架初始化环境所使用的内存):

What is php used to parse?

The above is the detailed content of What is php used to parse?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn