In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "the introduction of CLI command line operation mode of PHP". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "introduction to the CLI command line operation mode of PHP".
Analysis of CLI Command Line Operation Mode of PHP
When doing development, we not only do a variety of websites or interfaces, but also often need to write command-line scripts to handle some back-end transactions. Such as data processing statistics and so on. Of course, it is also for the sake of efficiency, when a transaction is likely to take a long time, it is often handed over to the server timer to call the script for processing, so that the client can have a better user experience. Let's take a look at PHP's command-line mode today, which is PHP CLI.
CLI and CGI
First, let's take a look at the difference between CLI and CGI. As we all know, Nginx uses FastCgi to invoke PHP's services. CGI is a general programming interface, that is, an interface for callers to use this program. The Nginx type of server does not run the PHP program directly, but executes the PHP program through FastCgi and gets the returned results.
CLI is Command Line Interface, that is, the command line interface. Mainly used as the development shell application of PHP. That is, using PHP to develop shell scripts. It is of course much more convenient than linux's native shell. In the command line state, you can run a piece of PHP code or an PHP file directly using the php command.
In addition, we can also use phpcgi to run a piece of PHP code or a PHP file directly on the command line, which is different from running it directly with the php command.
The output of CLI does not have any header information CLI does not change the working directory to the current directory of the script when CLI goes wrong the error message that outputs plain text (not in HTML format) forcibly overrides some settings in php.ini, because these settings are meaningless in the shell environment / / PHP CLI command line run mode analysis .php
Echo getcwd ()
Analysis of CLI Command Line running Mode of / / php-cgi dev-blog/php/202004/source/PHP .php
/ /. / MyDoc/ blog posts / dev-blog/php/202004/source
Analysis of CLI Command Line running Mode of / / php dev-blog/php/202004/source/PHP .php
/ /. / MyDoc/ blog posts
Let's choose the most typical example. In the file we run, we use getcwd () to output the directory where the current script is running, and we can see that the output results of the two running modes are significantly different. The php-cgi output is based on the directory where the file is located, while the php output is based on the directory where the command is currently running.
Run the PHP code directly
When doing some simple debugging, we can run a piece of code directly through 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. And this quote is more recommended to use single quotation marks, the following example will show why it is better to use single quotation marks.
CLI get parameters
Parameters can also be passed to the script in command line mode.
Analysis of CLI Command Line running Mode of / / PHP .php
Print_r ($argv)
/ / Analysis of CLI Command Line Operation 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 Operation Mode of php dev-blog/php/202004/source/PHP. Php 1 2 3
/ / Array
/ / (
Analysis of CLI Command Line Operation Mode of / / [0] = > 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 parameters on the command line are saved in the $argv variable, and there is also a $argc variable that holds the number of arguments.
We are still using php-cgi and php, two modes to test, from which we can see that the content printed by $argv in php-cgi mode turns out to be header information rather than specific parameter information. This is also true, after all, the CGI mode is originally an interface for the Web server, so it receives parameters such as post and get rather than command-line parameters.
We get the parameter contents normally in CLI mode, and $argv [0] always saves the current running file and path.
CLI Command Line Utility option
Finally, let's introduce some options that are commonly used on the command line.
-r parameters passed 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
/ / A pair of quotation marks ", 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 piece of code directly reports a warning when passing parameters to the CLI code running in double quotes. It's easy to understand that the $in double quotes will make the system's sh/bash think it's a variable and replace it with a variable parameter. Therefore, it is more recommended to use single quotes for day-to-day simple testing.
The second piece of code can print the contents of the passed parameters normally. The third line of code is that when you need to pass content with a-symbol, you need to give a-- parameter list delimiter first. This is because the contents of-xxx will make the php command think that this is a command option rather than a parameter, so we can add a delimiter to pass the parameter contents after the delimiter into the code as is.
Run PHP// php-an interactively
/ / php > $a = 1
/ / php > echo $a
/ / php > 1
Add a-an option and PHP will run interactively, and we can write code or run anything directly in an interactive state.
View phpinfo () and installed modules
These two should be the two options that you often use.
/ / output phpinfo ()
/ / php-I
/ / output modules loaded in PHP
/ / php-m
/ / View module details
/ / php-- ri swoole
In addition, we can also view the details of a specific extension module through the command-- ri module name. For example, here we can see the version of the swoole extension and related configuration information.
View a file / / display source code with comments and extra whitespace removed
Analysis of CLI Command Line Operation Mode of / / php-w dev-blog/php/202004/source/PHP .php
/ /
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.