Home  >  Article  >  Backend Development  >  php cli mode learning-PHP command line mode

php cli mode learning-PHP command line mode

不言
不言Original
2018-04-08 15:30:2412249browse

I knew before that php-cli mode is a shell-like command-style execution of PHP programs, but I always thought that this was a backward way and should be meaningless, because I have never encountered anyone using this cli mode for programming. But today I encountered an application using cli mode

Introduction to php_cli mode

php-cli is the abbreviation of php Command Line Interface, as its name means, it is php in The interface running on the command line is different from the PHP environment (php-cgi, isapi, etc.) running on the Web server. 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: php_cli mode is supported under windows and linux

PHP-cli application scenarios:

1. Multi-threaded application
The benefits of this aspect, 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 still 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 GUI 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 script
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!

PHP_CLI usage method

Win the following execution method:
Assume that php.exe is in D:xamppphp and the dos command can be executed like this:

Copy code The code is as follows:

D:\xamppphpphp.exe D:\xampphtdocstest.php



Just The file test.php can be executed. 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.

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

php cli mode learning-PHP command line mode

php is installed in the path /usr/local/ Under php/bin/php

Copy code The code is as follows:

/usr/local/php/bin/php /usr/local/ apache/htdocs/a.php



can execute a. php file

PHP_CLI programming needs to know
How to detect that the environment supports php_cli mode?

Copy code The code is as follows:

<?php
//方法1
if (PHP_SAPI === &#39;cli&#39;)
{
   // ...
}
//方法2
if (php_sapi_name() === &#39;cli&#39;)
{
   // ...
}



How does PHP_ClI receive 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);

get the following results:

php cli mode learning-PHP command line mode

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:

<?php 
function parseArgs($argv){ 
array_shift($argv); 
$out = array(); 
foreach ($argv as $arg){ 
if (substr($arg,0,2) == &#39;--&#39;){ 
$eqPos = strpos($arg,&#39;=&#39;); 
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) == &#39;-&#39;){ 
if (substr($arg,2,1) == &#39;=&#39;){ 
$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;


执行结果:

php cli mode learning-PHP command line mode

当然实现的方法不止一个,大家可以尝试其他方法实现!

例外关于php的cli还有很多参数可以加入:具体可以参考:http://php.net/manual/en/features.commandline.php

关于定时执行
cron是一个linux下的定时执行工具,可以在无需人工干预的情况下运行作业,周期性作业,比如备份数据 打开/etc/crontab,添加:

复制代码 代码如下:

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



关于corntab的详细使用参考51cto专题:Linux计划任务——cron服务

本文参考资料
http://www.php.cn/php-weizijiaocheng-312213.html

http://www.php.cn/php-weizijiaocheng-307840.html

http://www.php.cn/php-weizijiaocheng-307856.html

附注:2012-06-16 增加php_cli编程需知等

相关推荐:

vue cli升级webapck4的使用方法

The above is the detailed content of php cli mode learning-PHP command line mode. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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