Introduction to php_cli mode
php-cli is the abbreviation of php Command Line Interface. As its name means, it is the interface for php to run on the command line, which is different from that on the web server. Running PHP environment (php-cgi, isapi, etc.) In other words, PHP can not only write front-end web pages, it can also be used to write background programs. PHP CLI Shell Scripting applies all the advantages of PHP, enabling the creation of either server-side scripts or system or even applications with GUI! ——Note: Both windows and Linux support php_cli mode
PHP-cli application scenarios:
1. Multi-threaded application
Benefits in this area, quote Brother Niao’s words:
Advantages:
1. When using multiple processes, after the child process ends, the kernel will be responsible for recycling resources
2. When using multiple processes, the abnormal exit of the child process will not cause the entire process Thread Exit. The parent process still has a chance to rebuild the process.
3. A resident main process, only responsible for task distribution, the logic is clearer.
php multi-threading - yes, it is a php multi-threaded application, although Everyone generally believes that PHP does not have multi-threading (curl simulates multi-threading rather than real), but PHP in php_cli mode is completely multi-threaded. At this time, php belongs to a daemon process of Linux. When I wrote "PHP Multi-threaded Batch Collection and Download of Beauty Pictures (Continued)" before, although curl was used to simulate multi-threading in the collection program, execution timeout or memory abort would also be encountered when the browser executed it. Causes the program to be interrupted (it takes several attempts to completely succeed), but if you execute it in php-cli mode, you will find that the program executes very quickly, and the advantages of PHP multi-threaded execution are fully demonstrated.
Note: This multi-threading method is not very mature and is not suitable for large-scale generation applications. It can be used occasionally
2. Execute the php program regularly
I summarized the previous One of the three ways to use "PHP to execute scheduled tasks regularly" is to use the cron method of Linux. So how does this method execute PHP programs regularly? Please see below
3. Develop desktop applications
You can make your graphical user interface (GUI) applications in Windows or Linux using PHP! All you need is PHP's command line interface and a package of GTK. This will allow the creation of truly portable graphical user interface applications (haha, I only knew that php can be used as desktop programs before, but now I know that it uses php_cli mode), and there is no need to learn anything else.
4. Write PHP shell scripts
What should you do if you don’t know how to use bash shell or Perl, but you need some scripts to execute? At this time, you can completely write shell scripts using PHP that you are familiar with. At this time, do you suddenly feel that PHP is too powerful? ——Truly develop one language everywhere!
How to use PHP_CLI
Win the following execution method:
Assume that php.exe is in D:xamppphp and the dos command can be executed like this: