Php-cli mode Learning (php Command Line Mode)

Source: Internet
Author: User
Tags php multithreading

I know that the PHP-cli mode is a shell-like command-style execution of PHP programs, but I always thought this is a backward method and it should be meaningless, I have never encountered any programming using this CLI mode. However, I encountered an application using the CLI mode today.
Introduction to php_cli Mode

Php-CLI is short for PHP command line interface. It is called the interface that PHP runs on the command line. It is different from the PHP environment (PHP-CGI, ISAPI, etc.) that is to say, PHP can not only write foreground webpages, but also be used to write background programs. The CLI shell script of PHP is applicable to all the advantages of PHP. It enables creation of servers that support scripts, systems, and even GUI applications! -- Note: both Windows and Linux support the php_cli mode.
Php-cli application scenarios:
1. Multithreading

The following is a reference to laruence:

Advantages: 1. When a multi-process is used, the kernel will be responsible for recycling resources after the sub-process ends.

2. If a multi-process is used, abnormal exit of the sub-process will not cause the thread of the whole process to exit. The parent process also has the opportunity to rebuild the process.

3. A resident process is only responsible for task distribution, and the logic is clearer.

PHP multithreading-yes, it is a PHP multithreading application. Although we generally think that PHP does not have multithreading (curl is a simulated multithreading rather than a real one ), however, PHP in php_cli mode is completely multi-threaded. In this case, PHP is a Linux daemon. Although curl is used in the collection program to simulate multithreading, I wrote "php multi-thread batch collection and download of beautiful pictures (continued, however, during browser execution, the program may be interrupted due to execution timeout or memory abort (the program can be completely successful only after several attempts). However, if the program is executed in PHP-cli mode, you will find that this program runs very fast, and the advantages of PHP multi-thread execution are thoroughly demonstrated.

Note: This multithreading method is not very mature and is not suitable for large-scale application generation. It can be used occasionally.
2. regularly execute the PHP Program

I have summarized three methods of "php scheduled execution plan task". One of them is the Linux Cron method. How does this method regularly execute PHP programs? See the following
3. Develop desktop programs

You can use PHP graphical user interface (GUI) Applications in Windows or Linux! All you need is the PHP command line interface and a package of GTK. This will allow the establishment of real portable graphical user interface applications (Haha, I only knew that PHP can be used as a desktop program, and now I know that it is in php_cli mode), and do not need to learn anything else.
4. Compile the PHP shell script

If you do not use bash shell or Perl, but you need some scripts for execution, what should you do? At this time, you can use PHP to compile shell scripts that you are familiar with. Do you suddenly feel that PHP is too powerful! -- A real language for development everywhere!
How to Use php_cli
The following execution method is as follows:

Run the following command on the doscommand in D: \ XAMPP \ PHP:
1 D: \ XAMPP \ PHP \ php.exe D: \ XAMPP \ htdocs \ test. php

You can run the test. php file. We recommend the XAMPP Integration Environment on win platform, which is n times more powerful than Wamp. This integration package can directly enter the DOS mode.
Use php_cli in Linux

First, find the path for installing PHP. Take me as an example:

Image

Install php in the path/usr/local/PHP/bin/PHP
1/usr/local/PHP/bin/PHP/usr/local/Apache/htdocs/a. php

You can execute. PHP File
Php_cli Programming
How can I check whether the environment supports the php_cli mode?
1 <? PHP
2 // method 1
3 if (php_sapi = 'cli ')
4 {
5 //...
6}
7 // method 2
8 If (php_sapi_name () = 'cli ')
9 {
10 //...
11}

How does php_cli receive parameters?

By default, the/usr/local/PHP/bin/PHP receiving parameter is $ argv, which is fixed! Var_dump ($ argv) in the PHP file );

The following result is displayed:

Image

You can write a simple handler to convert this method to the common get/post parameter mode.

Simple code:
1 <? PHP
2
3 function parseargs ($ argv ){
4 array_shift ($ argv );
5 $ out = array ();
6 foreach ($ argv as $ Arg ){
7 if (substr ($ Arg, 0, 2) = '--'){
8 $ eqpos = strpos ($ Arg, '= ');
9 if ($ eqpos === false ){
10 $ key = substr ($ Arg, 2 );
11 $ out [$ key] = isset ($ out [$ key])? $ Out [$ key]: true;
12} else {
13 $ key = substr ($ Arg, 2, $ eqPos-2 );
14 $ out [$ key] = substr ($ Arg, $ eqpos + 1 );
15}
16} else if (substr ($ Arg, 0, 1) = '-'){
17 if (substr ($ Arg, 2, 1) = '){
18 $ key = substr ($ Arg, 1, 1 );
19 $ out [$ key] = substr ($ Arg, 3 );
20} else {
21 $ chars = str_split (substr ($ Arg, 1 ));
22 foreach ($ chars as $ char ){
23 $ key = $ char;
24 $ out [$ key] = isset ($ out [$ key])? $ Out [$ key]: true;
25}
26}
27} else {
28 $ out [] = $ ARG;
29}
30}
31 return $ out;
32}
33 var_dump ($ argv );
34 var_dump (parseargs ($ argv); exit;

Execution result:

Image

Of course there are more than one implementation method. You can try other methods to implement it!

For more information about the CLI of PHP, see: http://php.net/manual/en/features.commandline.php.
Scheduled execution

Cron is a scheduled execution tool in Linux. It can run jobs and periodic jobs without manual intervention, such as opening/etc/crontab for backup data and adding:

/Usr/bin/PHP-F/data/htdocs/test. php

For details about how to use corntab, refer to 51cto: Linux scheduled task-cron Service

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.