Not very understanding of the operating mechanism of PHP, solving

Source: Internet
Author: User
Like Java, Nodejs, should be on the server to continue to run a program, the program will run, will occupy the CPU, will consume memory, received network requests, will be processed.

and PHP does not seem to be a program, more like HTML, only open when the execution, when no one to access PHP services, PHP even a little bit of memory, a little CPU is not accounted for, is this it?

If so, wouldn't it be difficult for PHP to encapsulate the database class? Because each user comes in to give him to open a process, the process is not interlinked, a user come in to him open a database connection, B users come in and give him a separate database connection. That PHP is not constantly connecting and disconnecting the database? After a lot of users, the cost should be very large, right?

Does this cause PHP to have no JS-like setInterval or setTimeout function?

Reply content:

Like Java, Nodejs, should be on the server to continue to run a program, the program will run, will occupy the CPU, will consume memory, received network requests, will be processed.

and PHP does not seem to be a program, more like HTML, only open when the execution, when no one to access PHP services, PHP even a little bit of memory, a little CPU is not accounted for, is this it?

If so, wouldn't it be difficult for PHP to encapsulate the database class? Because each user comes in to give him to open a process, the process is not interlinked, a user come in to him open a database connection, B users come in and give him a separate database connection. That PHP is not constantly connecting and disconnecting the database? After a lot of users, the cost should be very large, right?

Does this cause PHP to have no JS-like setInterval or setTimeout function?

Take PHP-FPM as an example (like Apache mod_php) to talk about PHP's operating mechanism.
First, PHP-FPM is a multi-process fastcgi service implemented by C.
Each PHP-FPM worker process has a built-in PHP interpreter that does not rely on php-cgi and supports background residency, such as configuration:

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:9000pm = staticpm.max_children = 4[io];名为io的进程池监听9001端口,进程数常驻4个,最大8个listen = 127.0.0.1:9001pm = dynamicpm.max_children = 8pm.start_servers = 4pm.min_spare_servers = 4pm.max_spare_servers = 4

The PHP-FPM master process is used to manage these work processes, such as a worker process being killed and rebuilding one immediately.
PHP-FPM provides pm.status_path to view the working status of the entire PHP-FPM service through a browser.
Like executing timeout discards, automatically restarting worker processes after processing a specified number of requests, slow logging (identifying time-consuming files and functions) is supported.

And after the persistent connection is enabled, each PHP-FPM worker process can maintain a long connection to MySQL without releasing it, avoiding duplicate connections to the database.
For example, 2 PHP-FPM processes are shown with 2 long connections built by MySQL:

In addition, the PECL extensions for memcached and Redis are also capable of enduring connectivity.

It says the PHP-FPM system, and the following is the life cycle of the PHP script under the PHP-FPM system.
The lifetime of the amount in the PHP script (including the global variable) only exists in one request, and the request processing is completed, and PHP-FPM automatically frees the resource.
A request to release a resource once, this memory release is very thorough, PHP based on the reference count of the GC is not even functioning program is over.
The release of resources here refers to the resources controlled in the script, without interfering with the PHP-FPM resident process, will not let the PHP-FPM process shut down, and can not let PHP-FPM shut down to the persistent connection to MySQL, PHP-FPM is only responsible for input PHP script, perform PHP operation, If you join the Zendopcache support, you can also parse php script generated opcode all cached in memory for the next direct interpretation of execution. PHP7 also supports source code protection with Opcache.file_cache export script opcode.

Some common sense of PHP:
Database connection pool: PHP Persistent connection is a natural transparent connection pool without program intervention, support Mysql/memcached/redis and so on.
Memory resident: PHP-FPM process, zendopcache cached script of opcode, Yac developed by bird Brother are memory resident.
Garbage collection: PHP-FPM The run mode of a resource once a request is freed, weakening the role of GC based on reference counting.

Finally, PHP Daemon Services working under the CLI, such as Swoole/workerman, are similar to Java/node.

Although I have been off duty, see you this deeds PHP is similar html I can not endure. The PHP function is html incomparable good or bad, take you to say the connection database example, html can connect the data? Can I read the contents of the database? Can't do it! And you said that multiple users to access each user to open a database connection, you are too underestimated PHP . Encapsulating a database connection class It's simple and simple, and I'm not going to post it code . PHP manuals Each database extension has a description and examples, you can do your own reference. If you mention java that, you must know the singleton mode! PHPencapsulating the good one database connection class setting a singleton mode enables data connection statics. No matter how many users come to the call directly, you can open multiple processes without having multiple users to access them. And what you mentioned setInterval and setTimeout not the timing of execution! PHPYes, you can! First define the PHP to implement the function, and then by the server set on crontab it, the parameters to set the line, or there is php-fpm . And so on and so on the function I do not list, also did not organize the answer format you first live to see. I've got to go.

In short, PHP is a very good very good language, the function is very powerful ... But also in the continuous development and expansion of ... Easy to learn and understand Ah! Brother

PHP cannot reside in memory, every HTTP request is opened, the database is connected, the request ends, the database connection and various variables are released.

Your overall understanding is right, but not all languages are the kind of memory that resides in Java, python,go,c are not. PHP does not have the concept of a link pool, the process is finished to release, will not be saved to other processes with

To you, "deep understanding of the PHP kernel," chapter II of the first section of the Content link:

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

Your doubts, just because you only see the surface.

If you've ever used Swoole, I'm sure you won't ask this question.

Well, you're not a php-fpm.

and PHP does not seem to be a program, more like HTML

PHP can only be embedded in HTML, but with HTML is a big difference, PHP has a lot of extensions can do different things, HTML can?

Only open when the execution, when no one to access PHP services, PHP even a little bit of memory, a little CPU does not occupy, is it?

PHP has two main modes of operation (except the CLI):
1.Apache is the representative of the mod_php, then the work is given to Apache processing.
2.NGINX/LIGHTTPD PHP-FPM (fastcgi process Manager), this is the resident daemon.
So, how could it not account for CPU and memory?

If so, wouldn't it be difficult for PHP to encapsulate the database class? Because each user comes in to give him to open a process, the process is not interlinked, a user come in to him open a database connection, B users come in and give him a separate database connection. That PHP is not constantly connecting and disconnecting the database? After a lot of users, the cost should be very large, right?

In MySQL, for example, if you choose pconnect (persistent connection), the next time you use an existing connection, the connection will not be closed.
PDO also has a long connection option.

Does this cause PHP to have no JS-like setInterval or setTimeout function?

PHP has a delay function (such as sleep() ), and the accuracy is much higher than JS's too much (microsecond level).

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

In fact, I think for the landlord of the problem, PHP operating mechanism is to explain the execution script. PHP is the scripting language that provides service-side support for small and medium-sized websites, which is a good balance between development speed and operational efficiency, which is why it succeeds.

So the other views of the landlord did not have any interesting, even the controversy is meaningless, if PHP has the things you imagine, the language is no one to use, we simply still use Java good.

Landlord on the basic knowledge of PHP do not understand, it should be just beginning to contact PHP. In short, PHP can be run in two modes, one is script, such as executing PHP xxx.php in the shell, one is to understand the network request to start the operation (not very appropriate), there is compiled into Apache extension, there are fast-cgi ways. Apache's mod_php way is to come up with a request for a process execution, under efficiency, corresponding to a variety of parameter optimization. And fast-cgi is much more efficient, the concrete landlord can turn to see the corresponding manual and practical operation.

The upstairs few are really patient.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.