I don't quite understand the operating mechanism of PHP. Please give me a solution.

WBOY
Release: 2016-08-04 09:20:12
Original
1074 people have browsed it

Like java and nodejs, it should be a program that continuously runs on the server. This program will always run, occupy the CPU and memory, and will process the network request when it receives it.

PHP does not seem to be a program, it is more similar to HTML. It will only be executed when it is opened. When no one accesses the PHP service, PHP will not occupy even a little bit of memory or a little bit of CPU. Is that true?


If this is the case, isn’t it difficult for PHP to encapsulate database classes? Because each user comes in and has to open a process for him, and the processes are not connected to each other. User A comes in and opens a database connection for him, and user B comes in and opens a separate database connection for him. So isn't PHP constantly connecting and disconnecting from the database? After there are more users, the overhead should be very high, right?


Is this also the reason why PHP does not have js-like setInterval or setTimeout functions?

Reply content:

Like java and nodejs, it should be a program that continuously runs on the server. This program will always run, occupy the CPU and memory, and will process the network request when it receives it.

PHP does not seem to be a program, it is more similar to HTML. It will only be executed when it is opened. When no one accesses the PHP service, PHP will not occupy even a little bit of memory or a little bit of CPU. Is that true?


If this is the case, isn’t it difficult for PHP to encapsulate database classes? Because each user comes in and has to open a process for him, and the processes are not connected to each other. User A comes in and opens a database connection for him, and user B comes in and opens a separate database connection for him. So isn't PHP constantly connecting and disconnecting from the database? After there are more users, the overhead should be very high, right?


Is this also the reason why PHP does not have js-like setInterval or setTimeout functions?

Take PHP-FPM as an example (similar to Apache MOD_PHP) to talk about the operating mechanism of PHP.
First of all, PHP-FPM is a multi-process FastCGI service implemented in C.
Each PHP-FPM worker process has a built-in PHP interpreter. It does not rely on PHP-CGI and supports resident background, such as configuration:

<code>nginx.conf: 访问io.php的请求都交给监听9001的PHP-FPM进程池处理
location = /io.php {
    include fastcgi_params;
    fastcgi_pass 127.0.0.1:9001;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

php-fpm: 正常脚本由静态www池处理,阻塞脚本由动态io池处理
[www]
;名为www的进程池监听9000端口,常驻进程数量为固定4个
listen = 127.0.0.1:9000
pm = static
pm.max_children = 4
[io]
;名为io的进程池监听9001端口,进程数常驻4个,最大8个
listen = 127.0.0.1:9001
pm = dynamic
pm.max_children = 8
pm.start_servers = 4
pm.min_spare_servers = 4
pm.max_spare_servers = 4</code>
Copy after login

PHP-FPM main process is used to manage these working processes. For example, if a working process is killed, immediately recreate one.
PHP-FPM provides pm.status_path to view the work of the entire PHP-FPM service through the browser Status.
Such as execution timeout and giving up, automatically restarting the worker process after processing the specified number of requests, slow logging (finding out time-consuming files and functions) are all supported.

And after enabling persistent connections, each PHP-FPM working process can maintain a long connection to MySQL without releasing it, avoiding repeated connections to the database.
For example, the figure below shows two PHP-FPM processes and two MySQL connections. Long connection:
I don't quite understand the operating mechanism of PHP. Please give me a solution.
In addition, the PECL extension for Memcached and Redis also implements the function of persistent connection.

The PHP-FPM system is mentioned above, now let’s talk about the life cycle of PHP scripts under the PHP-FPM system.
The life cycle of the quantities in the PHP script (including global variables) only exists within one request, and the request is processed Once completed, PHP-FPM will automatically release the resources.
Resources are released once per request. This kind of memory release is very thorough. PHP's GC based on reference counting has ended before the program has even taken effect.
The release of resources here refers to It is the resource controlled in the script, and will not interfere with the PHP-FPM resident process, will not let the PHP-FPM process shut down, and will not let PHP-FPM close the persistent connection to MySQL. PHP-FPM is only responsible for inputting the PHP script. , to perform PHP operations, if ZendOpcache support is added, all opcodes generated by parsing PHP scripts can be cached in memory for direct interpretation and execution next time. PHP7 also supports using opcache.file_cache to export script opcodes to achieve source code protection.

Some common sense about PHP:
Database connection pool: PHP persistent connection is a natural and transparent connection pool that does not require program intervention, supporting MySQL/Memcached/Redis, etc.
Memory resident: PHP-FPM process, ZendOpcache cached script opcode , Yac developed by Brother Niao is all memory-resident.
Garbage collection: PHP-FPM’s operating mode of releasing resources once per request weakens the role of GC based on reference counting.

Finally, PHP daemon services such as Swoole/WorkerMan that work under CLI are similar to Java/Node.

Although I have already got off work, I couldn’t bear it when I saw that PHP is similar to html. The functions of PHP are incomparable to those of html. Take your example of connecting to a database, can html connect data? Can I read the contents of the database? No way! Also, you said that each user needs to open a database connection to access multiple users. You are looking down on PHP. Encapsulating a database connection class is very simple, so I won’t post the code. Each database extension in the PHP manual has an introduction and examples for your own reference. Since you mentioned java, you must also know the singleton pattern! PHPEncapsulate a database connection class and set up singleton mode to achieve static data connection. No matter how many users come to visit, just call it directly and you can open multiple processes instead of multiple users coming to visit as you said. And the setInterval and setTimeout you mentioned are not scheduled executions! PHP is also available! First define the functions to be implemented by PHP, and then set crontab on the server. Just set the parameters yourself, or php-fpm. I won’t list the functions, etc., and I haven’t formatted the answers yet, so you can take a look at them first. I have to get off work and leave. If we have any questions, we can continue discussing them tomorrow

In short, PHP is a very good language with very powerful functions... and it is still developing and growing... easy to learn and understand! Brother

PHP cannot reside in memory. After each HTTP request is opened, it connects to the database. When the request ends, the database connection and various variables are released.

Your overall understanding is correct, but not all languages ​​​​are memory-resident like java, neither python, go, nor c. PHP does not have the concept of a connection pool. The process will be released after it is executed and will not be left for other processes to use

Here is the link to the first section of Chapter 2 of "In-depth Understanding of the PHP Core":

http://www.php-internals.com/...

Your doubts are just because you only saw the surface.

If you have used swoole, I think you will not ask this question.

Uh, you thought php-fpm didn’t arrive. . .

And PHP doesn’t seem to be a program, it’s more similar to html

PHP can only be embedded into HTML, but it is completely different from HTML. PHP has many extensions that can do different things. Can HTML do it?

It will only be executed when it is opened. When no one accesses the PHP service, PHP will not occupy even a little bit of memory or a little bit of CPU. Is that right?

PHP mainly has two running modes (except CLI):
1. Mod_php represented by Apache, at this time the work is handed over to Apache.
2.php-fpm (fastcgi process manager) used by Nginx/Lighttpd, this is a resident daemon process.
So, how can it not occupy CPU and memory?

If this is the case, isn’t it difficult for PHP to encapsulate database classes? Because each user comes in and has to open a process for him, and the processes are not connected to each other. User A comes in and opens a database connection for him, and user B comes in and opens a separate database connection for him. So isn't PHP constantly connecting and disconnecting from the database? After there are more users, the overhead should be very high, right?

Take MySQL as an example. If you select pconnect (persistent connection) when connecting, the existing connection will be used directly next time and the connection will not be closed.
PDO also has a long connection option.

Is this also the reason why php does not have js-like setInterval or setTimeout functions?

PHP has delay functions (such as sleep()), and the accuracy is much higher than that of JS (microsecond level).

It’s so lively. @ivanilla's answers and comments contain a lot of information, thank you.

In fact, I think that in response to the original poster’s question, the operating mechanism of PHP is to interpret and execute scripts. PHP is a scripting language that provides server-side support for small and medium-sized websites. It has a good balance between development speed and operating efficiency. This is the reason for its success.

So the poster’s other views are of little value, and even the debate is meaningless. If PHP has the things you imagine, no one will use this language, and everyone might as well use Java.

The author does not know some basic knowledge of PHP. He should be new to PHP. To put it simply, PHP can be run in two modes. One is a script mode, such as executing php xxx.php in the shell; the other is to start running only when there is a network request (not very appropriate), and some are compiled into apache. Extension mode, there is also fast-cgi mode. Apache's mod_php method is to start a process for execution with a request. It is very efficient and has various parameter optimizations accordingly. Fast-cgi is much more efficient. Specific posters can read the corresponding manual and practice it.

The guys upstairs are so patient

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template