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 in the directory: D: xamppphp and doscommand:
The code is as follows:
D: \ xamppphpphp.exe D: \ xampphtdocstest. 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:
Install php in the path/usr/local/php/bin/php
The code is as follows:
/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?
The code is as follows:
// Method 1
If (PHP_SAPI = 'cli ')
{
//...
}
// Method 2
If (php_sapi_name () = 'cli ')
{
//...
}
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:
You can write a simple handler to convert this method to the common GET/post parameter mode.
Simple code:
The code is as follows:
Function parseArgs ($ argv ){
Array_shift ($ argv );
$ Out = array ();
Foreach ($ argv as $ arg ){
If (substr ($ arg, 0, 2) = '--'){
$ EqPos = strpos ($ arg, '= ');
If ($ eqPos = false ){
$ Key = substr ($ arg, 2 );
$ Out [$ key] = isset ($ out [$ key])? $ Out [$ key]: true;
} Else {
$ Key = substr ($ arg, 2, $ eqPos-2 );
$ Out [$ key] = substr ($ arg, $ eqPos + 1 );
}
} Else if (substr ($ arg, 0, 1) = '-'){
If (substr ($ arg, 2, 1) = '){
$ Key = substr ($ arg, 1, 1 );
$ Out [$ key] = substr ($ arg, 3 );
} Else {
$ Chars = str_split (substr ($ arg, 1 ));
Foreach ($ chars as $ char ){
$ Key = $ char;
$ Out [$ key] = isset ($ out [$ key])? $ Out [$ key]: true;
}
}
} Else {
$ Out [] = $ arg;
}
}
Return $ out;
}
Var_dump ($ argv );
Var_dump (parseArgs ($ argv); exit;
Execution result:
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:
The code is as follows:
/Usr/bin/php-f/data/htdocs/test. php
For details about how to use corntab, refer to 51cto: Linux scheduled task-cron service
References
Http://www.jb51.net/article/1716.htm
Http://www.jb51.net/article/37804.htm
Http://www.jb51.net/article/37796.htm
Note: added php_cli programming instructions on