I am not very familiar with the PHP operating mechanism.

Source: Internet
Author: User
For example, java and nodejs should all be a program that runs continuously on the server. this program will run continuously, Occupy cpu, occupy memory, and process requests from the network. PHP does not seem to be a program, and is more similar to html. it is executed only when it is opened. when no one accesses php... for example, java and nodejs should all be a program that runs continuously on the server. this program will run continuously, Occupy cpu, occupy memory, and process requests from the network.

PHP does not seem to be a program. it is more similar to html. it is executed only when it is opened. when no one accesses the php service, php does not occupy even a little memory, and a little cpu does not occupy it, is that true?

If so, isn't php difficult to encapsulate the database class? Because every user has to open A process for him, and processes do not communicate with each other, user A will open A database connection for him, and User B will open A separate database connection for him. Isn't php constantly connecting to or disconnecting the database? When there are too many users, should the overhead be high?

This also causes php to have no js-likesetIntervalOrsetTimeoutWhy is the function?

Reply content:

For example, java and nodejs should all be a program that runs continuously on the server. this program will run continuously, Occupy cpu, occupy memory, and process requests from the network.

PHP does not seem to be a program. it is more similar to html. it is executed only when it is opened. when no one accesses the php service, php does not occupy even a little memory, and a little cpu does not occupy it, is that true?

If so, isn't php difficult to encapsulate the database class? Because every user has to open A process for him, and processes do not communicate with each other, user A will open A database connection for him, and User B will open A separate database connection for him. Isn't php constantly connecting to or disconnecting the database? When there are too many users, should the overhead be high?

This also causes php to have no js-likesetIntervalOrsetTimeoutWhy is the function?

Taking PHP-FPM as an example (similar to Apache MOD_PHP) to talk about the operating mechanism of PHP.
First, PHP-FPM is a multi-process FastCGI service implemented by C.
Every PHP-FPM working process has built-in PHP interpreter, does not rely on the PHP-CGI, support background resident, such as configuration:

Nginx. conf: access io. php requests are all sent to the PHP-FPM process pool listening 9001 for location =/io. php {include fastcgi_params; fastcgi_pass 127.0.0.1: 9001; fastcgi_param SCRIPT_FILENAME $ document_root $ fastcgi_script_name;} php-fpm: normal scripts are processed by the static www pool, the blocking script is processed by the dynamic io pool [www]; the process pool named www listens to Port 9000, and the number of resident processes is fixed to four listen = 127.0.0.1: 9000pm = staticpm. max_children = 4 [io]; the process pool named io listens to Port 9001, and the number of processes is resident at 4, with a maximum of 8 listen = 127.0.0.1: 9001pm = dynamicpm. max_children = 8. start_servers = 4. min _spare_servers = 4. max_spare_servers = 4

The PHP-FPM master process is used to manage these work processes, such as a work process is killed, immediately re-build.
The PHP-FPM provides pm. status_path to view the working status of the entire PHP-FPM service in a browser.
Like giving up upon execution timeout, the worker process is automatically restarted after processing the specified number of requests. slow log records (find time-consuming files and functions) are supported.

And after enabling persistent connection, each PHP-FPM working process can maintain a persistent connection to MySQL is not released, avoid repeated connection to the database.
For example, it shows two persistent connections between two PHP-FPM processes and MySQL:

In addition, the PECL extension for Memcached and Redis also implements the persistent connection function.

Above said the PHP-FPM system, the following should talk about the PHP script in the PHP-FPM system of the lifecycle.
The life cycle of the amount (including global variables) in the PHP script only exists in one request, the request is processed completely, and the PHP-FPM automatically releases the resource.
One request to release resources once, this memory release is very thorough, and php gc based on reference count has even ended before it has even played a role.
Here the release of resources refers to the resources controlled in the script, instead of interfering with the PHP-FPM resident process, does not let the PHP-FPM process close, nor can let the PHP-FPM close to MySQL persistent connection, PHP-FPM is only responsible for entering the PHP script, execute PHP operations, if added to ZendOpcache support, you can also parse PHP script generated opcode cache to the memory for the next time directly explain the execution. opcache is also supported in PHP 7. file_cache export script opcode for source code protection.

Some common knowledge about PHP:
Database connection pool: PHP persistent connections are naturally transparent connection pools without Program intervention, and support for MySQL, Memcached, and Redis.
Memory resident: PHP-FPM process, ZendOpcache cache script opcode, bird brother developed Yac are memory resident.
Garbage collection: The Running mode of a resource is released by a PHP-FPM with one request, which weakens the function of GC based on reference count.

Finally, the PHP daemon service that works in CLI, such as Swoole/WorkerMan, is similar to Java/Node.

Although I have been off work, I can tell you this.PHPIs similarhtmlI cannot bear it.PHPFunction ishtmlIf it is incomparable, take the database connection example you mentioned as follows,htmlCan data be connected? Can I read database content? No! In addition, you may say that multiple users need to establish a database connection to access each user.PHP. It's easy to encapsulate a database connection class.code. The PHP Manual provides an introduction and examples for each type of database extension. you can refer to it by yourself. Since you mentionedjavaYou must also know the Singleton mode!PHPEncapsulate a single-instance mode for database connection settings to achieve static data connection. No matter how many users access the service, multiple processes can be started without multiple users. And what you mentionedsetIntervalAndsetTimeoutIsn't it scheduled execution!PHPYes! First, define the functions to be implemented by PHP, and then set them on the server.crontabYou can set the parameters yourself, orphp-fpm. I will not list other functions, or sort out the answer format. I have to leave from work. if there is a problem, we will continue to discuss it tomorrow.

In short,PHPIt is a very good language with powerful functions ...... In addition, we are constantly developing ...... Easy to learn and understand! Brother

PHP cannot stay in the memory. after each HTTP request is opened, it connects to the database. when the request ends, the database connection and various variables are released.

You are correct in general, but not all languages are java's resident memory. python, go, and c are not. PHP does not have the concept of a connection pool. after the process is executed, it will be released and will not be used by other processes.

Link to Chapter 2 Section 1 of "deep understanding of PHP kernel:

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

Your doubts are only because you only see the surface.

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

Well, you didn't arrive at php-fpm...

PHP does not seem to be a program, it is more similar to html

PHP can only be embedded in HTML, but it is a world different from HTML. PHP has many extensions that can do different things. can HTML?

It is executed only when it is enabled. when no one accesses the php service, php does not occupy even a little bit of memory, and a little bit of cpu does not occupy. Is that true?

PHP runs in two ways (except CLI ):
1. mod_php, represented by Apache, is handed over to Apache for processing.
2. Nginx/Lighttpd uses the php-fpm (fastcgi Process Manager), which is a resident daemon.
So, how can I not occupy the CPU and memory?

If so, isn't php difficult to encapsulate the database class? Because every user has to open A process for him, and processes do not communicate with each other, user A will open A database connection for him, and User B will open A separate database connection for him. Isn't php constantly connecting to or disconnecting the database? When there are too many users, should the overhead be high?

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

This also causes php to have no js-likesetIntervalOrsetTimeoutWhy is the function?

PHP has a latency function (suchsleep()), And the accuracy is much higher than that of JS (in microseconds ).

So busy. @ Ivanilla the answers and comments contain a lot of information. thank you.

In fact, I think the PHP operating mechanism is to explain the execution script to solve the problem of the landlord. PHP is a script language that provides server support for small and medium-sized websites. it has a good balance between development speed and running efficiency, which is the reason for its success.

Therefore, the author's other points of view will not look at the head, and even the argument will be meaningless. If PHP has what you think, no one will use this language, and everyone will simply use java.

I am not familiar with some basic PHP knowledge. I should have started to get started with PHP. In simple terms, PHP can run in two modes, one is the script mode, such as executing php xxx in shell. php. One is that the landlord understands that network requests are used to start running (not very appropriate), there are extensions compiled into apache, and there are also fast-cgi methods. Apache's mod_php method is to initiate a process for execution with a request. under the efficiency, there are various parameter optimizations. Fast-cgi is much more efficient. you can refer to the corresponding manual and perform the operations.

Several upstairs 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.