Analysis of CLI command line running mode of PHP

Time:2021-9-20

When doing development, we not only do various websites or interfaces, but also often need to write some command-line scripts to deal with some back-end transactions. For example, data processing and statistics. Of course, for the sake of efficiency, when a transaction may consume a long time, it is often handed over to the timer of the server to call the script for processing at a fixed time, so that the client can have a better user experience. Today, let’s learn about the command line operation mode of PHP, that is, PHP cli.

CLI and CGI

First, let’s look at the difference between CLI and CGI. As we all know, nginx uses fastcgi to call PHP services. CGI is a general programming interface, that is, an interface for callers to use this program. Nginx servers do not run PHP programs directly. Instead, they execute PHP programs and get the returned results through fastcgi.

CLI is command line interface, that is, command line interface. It is mainly used as the development shell application of PHP. That is to use PHP to develop shell scripts. Compared with the native shell of Linux, it is of course much more convenient. In the command-line state, you can run a piece of PHP code or a PHP file directly using the PHP command.

In addition, we can also directly use phpcgi to run a piece of PHP code or a PHP file on the command line. What’s the difference between it and directly using PHP commands?

  • The CLI output does not have any header information
  • When the CLI is running, it does not change the working directory to the current directory of the script
  • Output plain text error message (non HTML format) when cli error occurs
  • Some settings in php.ini are forcibly overridden because they are meaningless in a shell environment
//Analysis of CLI command line running mode of PHP
echo getcwd();

//Analysis of CLI command line running mode of PHP CGI dev blog / PHP / 202004 / source / php.php
//..... / MyDoc / blog post / dev blog / PHP / 202004 / source

//Analysis of CLI command line running mode of PHP dev blog / PHP / 202004 / source / PHP
//..... / MyDoc / blog post

We select the most typical example. In the file we run, we use getcwd () to output the directory where the current script runs. We can see that the output results of the two running modes are obviously different. PHP CGI is output based on the directory where the file is located, while PHP is output based on the directory where the command is currently running.

Run PHP code directly

When doing some simple debugging, we can run a piece of code directly through the CLI.

// php -r "echo 121;"
// 121

That is, simply add a – R parameter followed by a piece of code, which must be enclosed in quotation marks. Moreover, single quotation marks are recommended for this quotation mark. The following examples will show why single quotation marks are better.

CLI get parameters

Parameters can also be passed to scripts in command line mode.

//Analysis of CLI command line running mode of PHP
print_r($argv);
//Analysis of CLI command line running mode of PHP CGI dev blog / PHP / 202004 / source / php.php 1 2 3
// X-Powered-By: PHP/7.3.0
// Content-type: text/html; charset=UTF-8

//Analysis of CLI command line running mode of PHP dev blog / PHP / 202004 / source / php.php 1 2 3
// Array
// (
//[0] = > analysis of CLI command line operation mode of dev blog / PHP / 202004 / source / php.php
//     [1] => 1
//     [2] => 2
//     [3] => 3
// )

In the test file, we printed the \ $argv variable. When the PHP script runs, all the parameters of the command line will be saved in the $argv variable, and a $argc variable will save the number of parameters.

We still use PHP CGI and PHP to test. From here, we can find that the content printed by $argv in PHP CGI mode is header information rather than specific parameter information. This is also true. After all, CGI mode is originally an interface provided for the web server, so it receives parameters such as post and get rather than command-line parameters.

In cli mode, we normally get the parameter content, and $argv [0] always saves the current running file and path.

CLI command line utility options

Finally, we will introduce some common options on the command line.

-R parameter passing when directly running code

// php -r "var_dump($argv);" app 
// Warning: var_dump() expects at least 1 parameter, 0 given in Command line code on line 1
//Double quotation mark ", SH / bash implements parameter substitution

// php -r 'var_dump($argv);' app
// array(2) {
//     [0]=>string(19) "Standard input code"
//     [1]=>string(3) "app"
// }

// php -r 'var_dump($argv);' -- -h
// array(2) {
//     [0]=>string(19) "Standard input code"
//     [1]=>string(2) "-h"
// }

The first code will directly report a warning when passing parameters to the CLI code running in double quotation marks. In fact, it is easy to understand that the $in double quotation marks will make the SH / bash of the system think that this is a variable, so as to replace the variable parameters. Therefore, it is more recommended to use single quotation marks for daily simple testing.

The second code can normally print the parameters passed in. The third line of code is that when you need to pass the content with – symbol, you need to give a – parameter list separator first. This is because the content of – XXX will make the PHP command think that this is a command option rather than a parameter, so we can add a separator to pass the parameter content after the separator into the code as it is.

Run PHP interactively

// php -a
// php > $a = 1;
// php > echo $a;
// php > 1

Add a – a option and PHP will run interactively. We can write code or run anything directly in the interactive state.

Check phpinfo() and installed modules

These two options should be used frequently.

//Output phpinfo ()
// php -i

//Output modules loaded in PHP
// php -m

//View module details
// php --ri swoole

In addition, we can use the — RI module name command to view the details of a specific extension module. For example, here we can view the version of the swoole extension and related configuration information.

View a file

//Displays the source code with comments and extra white space removed
//Analysis of CLI command line running mode of PHP -w dev blog / PHP / 202004 / source / php.php
// <?php
//  echo getcwd(); print_r($argv);

//Read input through Linux pipeline
//Analysis of the CLI command line running mode of cat dev blog / PHP / 202004 / source / PHP. PHP | PHP - R "print file_get_contents (' php://stdin ');"
//... everything in this document

The last two tips, one is that through the – W option, we can print all non comment and line feed contents in this PHP file. It can be regarded as the same ability as the front-end code compression. There are many comments in our test file. After passing this command, we print out the result of removing all comments and blank lines.

The other is that we can send data to PHP cli through Linux pipeline. Here, we view our test file through cat, and then send it to PHP cli through pipeline. In the script, we use stdin to read the content sent by pipeline, and complete the printing of the whole file content. We don’t do any filtering here, so we print the contents of the whole file. You can run this command to test.

summary

In fact, there are many options when running in command line mode. Here we just select some very useful contents for display. Of course, most frameworks provide a script framework for the command line. For example, in laravel, you can create a command line script through PHP artisan make: command, and then use PHP artisan to run the script in the framework. These contents will be explained in detail in the future.

Command line cli mode is widely used in almost any project, so we will benefit greatly from in-depth study and mastery of it.

Test code:

https://github.com/zhangyue0503/dev-blog/blob/master/php/202004/source/PHP%E7%9A%84CLI%E5%91%BD%E4%BB%A4%E8%A1%8C%E8%BF%90%E8%A1%8C%E6%A8%A1%E5%BC%8F%E6%B5%85%E6%9E%90.php

Reference documents:

https://www.php.net/manual/zh/features.commandline.php

Each media platform can search [hard core project manager]