Phpcli mode learning (PHP command line mode) _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Tags php multithreading
Phpcli mode learning (PHP command line mode ). Php_cli mode description php-cli is short for phpCommandLineInterface, as it means, it is the interface for php to run on the command line, different from the php environment running on the Web server ( 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

Php Command Line Interface (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 running on the Web server (...

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.