When we do development, we not only do various websites or interfaces, but also often need to write some command line scripts to handle some back-end transactions. For example, data processing statistics. Of course, for the sake of efficiency, when a transaction may take 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 we’ll take a look at the PHP command-line mode, also known as THE PHP CLI.

CLI with CGI

First, let’s look at the differences between CLI and CGI. As we all know, Nginx uses FastCgi to invoke PHP services. CGI is a generic programming interface, that is, an interface provided to the caller to use this program. Instead of running PHP programs directly, Nginx servers use FastCgi to execute PHP programs and get results back.

The CLI is the Command Line Interface. Mainly used as PHP development shell application. That is to use PHP for shell script development. It’s certainly much more convenient than the native Linux shell. From the command line, you can run a piece of PHP code or a PHP file directly using the PHP command.

In addition, we can run PHP code or a PHP file directly from the command line using phpcgi. How is this different from running PHP commands directly?

  • The CLI output does not contain any header information
  • The CLI does not change the working directory to the current directory of the script
  • Output plain text error messages (not HTML) when CLI error occurs
  • Some Settings in php.ini are forcibly overwritten because they do not make sense in a shell environment
// PHP command-line interface (CLI
echo getcwd();

/ dev/PHP - cgi - blog/PHP / 202004 / source/PHP CLI command line mode is analysed. The PHP
/ /... /MyDoc/ blogpost /dev-blog/ PHP /202004/source

/ / PHP dev blog/PHP / 202004 / source/PHP CLI command line mode is analysed. The PHP
/ /... /MyDoc/ blog post
Copy the code

Taking the most typical example, we run a file that uses getcwd() to output the directory in which the script is currently running, and you can see the obvious difference between the two runs. Php-cgi bases output on the directory where the file is located, whereas PHP bases output on the directory where the command is currently running.

Run the PHP code directly

For some simple debugging, you can run a piece of code directly through the CLI.

// php -r "echo 121;"
/ / 121
Copy the code

Simply add a -r argument followed by a line of code that must be enclosed in quotes. Single quotes are recommended for this quote, and the following examples will show why single quotes are better.

CLI Obtaining Parameters

It is also possible to pass parameters to scripts in command line mode.

// PHP command-line interface (CLI
print_r($argv);
/ dev/PHP - cgi - blog/PHP / 202004 / source/PHP CLI command line mode is analysed. PHP 1 2 3
/ / X - Powered By: PHP / 7.3.0
// Content-type: text/html; charset=UTF-8

/ / PHP dev blog/PHP / 202004 / source/PHP CLI command line mode is analysed. PHP 1 2 3
// Array
/ / (
/ / [0] = > dev blog/PHP / 202004 / source/PHP CLI command line mode is analysed. The PHP
/ / [1] = > 1
/ / [2] = > 2
/ / [3] = > 3
// )
Copy the code

In the test file, we print the variable \ argv. When the PHP script runs, it saves all the arguments to the command line in the argv variable. When the PHP script runs, it saves all the arguments to the command line in the argv variable. When the PHP script runs, it stores all the arguments to the command line in the argv variable, and there is also a $argc variable that holds the number of arguments.

$argv: $argv: $argv: $argv: $argv: $argv: $argv: $argv: $argv: $argv: $argv: $argv: $argv: $argv: $argv: $argv: $argv: $argv: $argv: $argv: $argv: $argv: $argv: $argv: $argv After all, the CGI mode is supposed to provide an interface to the Web server, so it takes parameters like POST and GET rather than command line parameters.

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

CLI command line utility options

Finally, let’s look at some of the options commonly used on the command line.

-r Passes parameters when running code directly

// php -r "var_dump($argv);" app 
// Warning: var_dump() expects at least 1 parameter, 0 given in Command line code on line 1
// double quotes ", 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"
// }
Copy the code

The first line of code will directly warn you when passing arguments to CLI code running double quotes. It makes sense that the $in the double quotation marks will trick the sh/bash system into thinking it is a variable and substituting variable arguments. Therefore, it is recommended to use single quotes for daily simple tests.

The second piece of code prints the content of the arguments passed in as normal. The third line of code requires a — argument list delimiter when passing content with the – sign. This is because the contents of -xxx would make the PHP command think of it as a command option rather than an argument, so we add a delimiter so that the argument content after the delimiter is passed into the code as is.

Run PHP interactively

// php -a
// php > $a = 1;
// php > echo $a;
// php > 1
Copy the code

By adding the -a option, PHP runs interactively, and you can write code or run anything in the interactive state directly.

View phpInfo () and installed modules

These are the two choices that you’ll probably use a lot.

/ / output a phpinfo ()
// php -i

// Outputs the modules loaded in PHP
// php -m

// View module details
// php --ri swoole 
Copy the code

You can also use the –ri module name command to view details about a specific extension module. Here, for example, you can view the version of the Swoole extension and its configuration information.

Viewing a file

// Displays the source code with comments and extra white space removed
/ dev/PHP - w - blog/PHP / 202004 / source/PHP CLI command line mode is analysed. The PHP
// 
       
// echo getcwd(); print_r($argv);

// Read input through a Linux pipe
/ / the cat dev blog/PHP / 202004 / source/PHP CLI command line mode is analysed. The PHP | PHP - r "print file_get_contents (' PHP: / / stdin);"
/ /... Everything in this file
Copy the code

Two final tricks, one is that with the -w option, we can print all the non-commented and newline content in this PHP file. Think of it as a front-end code compression capability. We have a lot of comments in the test file, and what we print out after using this command is the result of removing all comments and blank lines.

The other is that we can send data to the PHP CLI using Linux pipes. Here we view our test file via cat and pipe it to the PHP CLI, using STDIN in the script to read the pipe to finish printing the contents of the entire file. We’re not doing any filtering here, so we’re printing the entire file, so you can test it by running this command.

conclusion

There are a lot of options when running in command line mode, but here we have selected some of the most useful ones. Of course, most frameworks provide scripting frameworks for the command line. For example, in Laravel, you can create command-line scripts using PHP Artisan make: Command, and then run scripts from the framework using PHP Artisan. This is something that we’ll talk about in more detail in the future in terms of learning frameworks.

The command-line interface (CLI) mode is widely used in almost any project, so learning it in depth is a great benefit.

Test code:

Github.com/zhangyue050…

Reference Documents:

www.php.net/manual/zh/f…