Home > Backend Development > PHP Tutorial > Learning php cli mode (PHP command line mode)_PHP tutorial

Learning php cli mode (PHP command line mode)_PHP tutorial

WBOY
Release: 2016-07-21 15:09:58
Original
1203 people have browsed it

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:

Copy the code The code is as follows:
D:xamppphpphp.exe D:xampphtdocstest.php


and you can execute it test.php file. Here we recommend the xampp integrated environment under the win platform, which is truly N times more powerful than wamp. This integrated package can directly enter dos mode.

Use php_cli under Linux
First find the path where you installed php, take me as an example:

Learning php cli mode (PHP command line mode)_PHP tutorial

php is installed in the path /usr/local/ Copy the code under php/bin/php

Copy the code The code is as follows:
/usr/local/php/bin/php /usr /local/apache/htdocs/a.php


can execute a. php file

What you need to know about PHP_CLI programming
How to detect that the environment supports php_cli mode?

Copy the code The code is as follows:

//Method 1
if (PHP_SAPI === 'cli')
{
// ...
}
//Method 2
if (php_sapi_name() === 'cli')
{
// ...
}


How to receive PHP_ClI Parameters?
By default, the parameter received by /usr/local/php/bin/php is $argv. This variable is fixed! In the php file, var_dump($argv);

gets the following results:

Learning php cli mode (PHP command line mode)_PHP tutorial

You can write a simple processing function to convert this method into a commonly used one. Parameter mode for GET/post.

Simple code:
Copy 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:

Learning php cli mode (PHP command line mode)_PHP tutorial

Of course, there is more than one way to achieve it, You can try other methods to achieve it!

Exception There are many parameters that can be added to the cli of php: for details, please refer to: http://php.net/manual/en/features.commandline.php

About scheduled execution
Cron is a scheduled execution tool under Linux that can run jobs without manual intervention. For periodic jobs, such as backing up data, open /etc/crontab and add:

Copy the code The code is as follows:

/usr/bin/php -f /data/htdocs/test.php


For detailed use of corntab, please refer to the 51cto topic: Linux scheduled tasks - cron service

Reference materials for this article
http://www.jb51.net/article/1716.htm
http:// www.jb51.net/article/37804.htm
http://www.jb51.net/article/37796.htm

Note: 2012-06-16 Added php_cli programming requirements, etc.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327206.htmlTechArticleIntroduction to php_cli mode php-cli is the abbreviation of php Command Line Interface, as its name means, it is php in the command The interface that runs on the line is different from the php environment running on the web server (...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template