Table of Contents
A brief analysis of PHP's CLI command line operating mode
Home Backend Development PHP Problem What does php cli mean?

What does php cli mean?

Feb 28, 2022 pm 06:11 PM
cli php

In php, the full name of cli is "Command Line Interface", which means "command line interface". It is a command line running mode, mainly used as a development shell application for PHP, that is, using PHP for shell Development of scripts.

What does php cli mean?

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

In php, the full name of cli is "Command Line Interface ", meaning "command line interface", is a command line running mode.

A brief analysis of PHP's CLI command line operating mode

When doing development, we not only do various websites or interfaces, but also often need to write some commands Line scripts are used to handle some back-end transactions. For example, data processing and statistics. Of course, for the sake of efficiency, when a transaction may take a long time, the server's timer is often used to call the script at a fixed time for processing, so that the client can have a better user experience. Today we will learn about PHP’s command line running mode, which is PHP CLI.

CLI and CGI

First let’s look at the difference between CLI and CGI. We all know that Nginx uses FastCgi to call PHP services. CGI is a universal programming interface, which is an interface provided to the caller to use this program. This type of server, Nginx, does not run PHP programs directly, but uses FastCgi to execute PHP programs and obtain return results.

CLI is the Command Line Interface, which is the command line interface. Mainly used as a development shell application for PHP. That is, use PHP to develop shell scripts. Compared with the native Linux shell, it is of course much more convenient. In the command line state, you can run a certain PHP code or a certain PHP file directly using the php command.

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

  • The output of CLI does not have any header information

  • CLI will not change the working directory to the current directory of the script when running

  • CLI outputs plain text error messages (non-HTML format) when an error occurs

Forcibly overrides certain settings in php.ini because these settings It is meaningless in a shell environment

// PHP的CLI命令行运行模式浅析.php
echo getcwd();

//  php-cgi dev-blog/php/202004/source/PHP的CLI命令行运行模式浅析.php
// ...../MyDoc/博客文章/dev-blog/php/202004/source

// php dev-blog/php/202004/source/PHP的CLI命令行运行模式浅析.php
Copy after login

We choose the most typical example. In the file we run, we use getcwd() to output the directory where the current script is running. It can be seen that the output of the two running methods The results are markedly different. php-cgi outputs based on the directory where the file is located, while php outputs based on the directory where the command is currently run.

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
Copy after login

That is, simply add the -r parameter, followed by a piece of code, which must be enclosed in quotation marks. And it is recommended to use single quotes for this quote. The following examples will show why single quotes are better.

CLI Get parameters

You can also pass parameters to the script in command line mode.

// PHP的CLI命令行运行模式浅析.php
print_r($argv);
// php-cgi dev-blog/php/202004/source/PHP的CLI命令行运行模式浅析.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命令行运行模式浅析.php 1 2 3
// Array
// (
//     [0] => dev-blog/php/202004/source/PHP的CLI命令行运行模式浅析.php
//     [1] => 1
//     [2] => 2
//     [3] => 3
// )
Copy after login

In the test file, we printed the \$argv variable. When the PHP script is run, all parameters of the command line will be saved in the $argv variable, and there is also an $argc variable that will save the number of parameters.

We still use php-cgi and php, two modes for testing. From here we can find that the content printed by $argv in php-cgi mode is actually header information, not specific parameter information. This is correct, after all, the CGI mode is originally an interface provided for the web server, so it receives parameters such as post and get instead of command line parameters.

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

CLI command line practical options

Finally, we will introduce some commonly used options in the command line.

  • -r Parameter passing when running the 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
// 双引号 ",sh/bash 实行了参数替换

// 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 after login

The first piece of code is passing parameters to the CLI code running in double quotes When the time comes, a warning will be reported directly. In fact, it is easy to understand. The $ in double quotes will make the system's sh/bash think that this is a variable and replace the variable parameters. Therefore, it is more recommended to use single quotes for daily simple testing.

The second piece of code can print the passed parameter content normally. The third line of code requires that when content with the - symbol needs to be passed, a -- parameter list separator needs to be given 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 add a delimiter to pass the parameter content after the delimiter into the code as it is.

Run PHP interactively

// php -a
// php > $a = 1;
// php > echo $a;
// php > 1
Copy after login

Add a -a option, PHP will run interactively, we can write code directly in the interactive state or Run anything.

View phpinfo() and installed modules

这两个应该是大家经常会使用的两个选项。

// 输出 phpinfo()
// php -i

// 输出 PHP 中加载的模块
// php -m

// 查看模块详细信息
// php --ri swoole
Copy after login

另外我们还可以通过 --ri 模块名 这个命令来查看具体某个扩展模块的详细信息。比如这里我们可以查看到 swoole 扩展的版本及相关的配置信息。

查看某个文件

// 显示去除了注释和多余空白的源代码
// php -w dev-blog/php/202004/source/PHP的CLI命令行运行模式浅析.php
// <?php
//  echo getcwd(); print_r($argv);
// 通过 linux 管道读取输入
// cat dev-blog/php/202004/source/PHP的CLI命令行运行模式浅析.php | php -r "print file_get_contents(&#39;php://stdin&#39;);"
// ......这个文件里面所有的内容
Copy after login

最后两个小技巧,一个是通过 -w 选项,我们可以打印这个 php 文件中所有非注释和换行的内容。可以看成是像前端的代码压缩一样的能力。我们这个测试文件中有非常多的注释,通过这个命令后我们打印出来的内容是去除掉所有注释和空白行的结果。

另一个是我们可以用 linux 管道的方式向 PHP CLI 发送数据。这里我们通过 cat 查看我们的测试文件然后通过管道发送给 PHP CLI,在脚本中使用 STDIN 来读取管道发送过来的内容完成了整个文件内容的打印。这里我们没进行任何过滤,所以打印的是整个文件里面的内容,大家可以运行这个命令来测试。

总结

其实命令行模式运行的时候还有很多的选项,这里我们只是选取了一部分非常有用的内容进行展示。当然,大部分框架都提供了用于命令行的脚本框架,比如 laravel 中可以通过 php artisan make:command 来创建命令行脚本,然后使用 php artisan 来运行框架中的脚本。这些内容将来我们在学习框架方面知识的内容将会进行详细的讲解。

命令行 CLI 模式的应用非常广泛,几乎任何项目中都会使用到,所以,深入的学习掌握它将会使我们大受裨益。

推荐学习:《PHP视频教程

The above is the detailed content of What does php cli mean?. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1669
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

See all articles