Home > Backend Development > PHP Tutorial > PHP command line

PHP command line

炎欲天舞
Release: 2023-03-15 12:44:02
Original
11398 people have browsed it


  • Detailed explanation of php command line (CLI) parameters

    • Run php in interactive shell mode

    • Run the built-in web server

    • Find the PHP configuration file

    • View classes/functions/ Extended information

    • Syntax check

  • Command line script development

PHP as a As a web development language, we usually run PHP in the Web Server and access it using a browser, so we rarely pay attention to its command line operations and the use of related parameters. However, especially on Unix-like operating systems, PHP can As a scripting language, it performs processing tasks similar to those of a shell.

Detailed explanation of php command line (CLI) parameters

To view all command line parameters of PHP, use the php -h command. We will explain most of the commonly used command line parameters one by one to deepen our understanding of PHP's capabilities, use PHP on the server command line more quickly or debug various problems that arise due to unfamiliarity with the environment.

-a               以交互式shell模式运行
-c | 指定php.ini文件所在的目录
-n               指定不使用php.ini文件
-d foo[=bar]     定义一个INI实体,key为foo,value为'bar'
-e               为调试和分析生成扩展信息
-f         解释和执行文件.
-h               打印帮助
-i               显示PHP的基本信息
-l               进行语法检查 (lint)
-m               显示编译到内核的模块
-r         运行PHP代码,不需要使用标签 ..?>
-B   在处理输入之前先执行PHP代码
-R         对输入的没一行作为PHP代码运行
-F         Parse and execute  for every input line
-E     Run PHP  after processing all input lines
-H               Hide any passed arguments from external tools.
-S : 运行内建的web服务器.
-t      指定用于内建web服务器的文档根目录
-s               输出HTML语法高亮的源码
-v               输出PHP的版本号
-w               输出去掉注释和空格的源码
-z         载入Zend扩展文件 .
 
args...          传递给要运行的脚本的参数. 当第一个参数以-开始或者是脚本是从标准输入读取的时候,使用--参数
 
--ini            显示PHP的配置文件名
 
--rf       显示关于函数  的信息.
--rc       显示关于类  的信息.
--re       显示关于扩展  的信息.
--rz       显示关于Zend扩展  的信息.
--ri       显示扩展  的配置信息.
Copy after login

The above lists all the parameters of the PHP command and their comments. Next, we will give examples of the more commonly used parameters.

Run php in interactive shell mode

Friends who have used Python are familiar with Python’s interactive shell. Under the command line, if we directly enter the python command, we will enter python. Interactive shell program, you can then perform some computing tasks interactively.

In the PHP command line, similar functions are also provided. Use the -a parameter to enter interactive shell mode.

In this shell, we can perform some simple tasks without always creating a new php file.

For more detailed instructions, please refer to the official documentation

Running the built-in Web server

Starting from PHP 5.4.0, PHP’s command line mode provides a built-in Built web server. Use -S to start running the web service.

Assume that we are currently in the directory /Users/mylxsw/codes/php/aicode/demo. In this directory, there is an index.php file.

$ ls
index.php
$ cat index.php
<?php echo "Hello, PHPER!";
Copy after login

In this directory, execute the following command to start the built-in web server, and use the current directory as the working directory by default

$ php -S localhost:8000
PHP 5.6.3 Development Server started at Wed Jun 10 15:49:41 2015
Listening on http://localhost:8000
Document root is /Users/mylxsw/codes/php/aicode/demo
Press Ctrl-C to quit.
Copy after login

We open another shell window, Request http://localhost:8000/ to see the script output

$ curl -is http://localhost:8000/
HTTP/1.1 200 OK
Host: localhost:8000
Connection: close
X-Powered-By: PHP/5.6.3
Content-type: text/html;
 
Hello, PHPER!
Copy after login

In the window where the web service is running, you can see the output log information

Above we are starting When building the server, only the -S parameter was specified to let PHP run as a web server. At this time, PHP will use the current directory as the working directory, so return to the current directory to find the requested file. We can also use the -t parameter. Specify another directory as the working directory (document root).

For more details, please refer to the official documentation.

Find the PHP configuration file

Sometimes, due to the confusion of software installation on the server, we may have installed multiple versions of the PHP environment. At this time, how to locate our PHP program? Which configuration file is used is more important. In the PHP command line parameters, the –ini parameter is provided. Using this parameter, you can list the current PHP configuration file information.

$ php --ini
Configuration File (php.ini) Path: /usr/local/etc/php/5.6
Loaded Configuration File:         /usr/local/etc/php/5.6/php.ini
Scan for additional .ini files in: /usr/local/etc/php/5.6/conf.d
Additional .ini files parsed:      (none)
 
$ /usr/local/php/bin/php --ini
Configuration File (php.ini) Path: /usr/local/php/etc/
Loaded Configuration File:         /usr/local/php/etc/php.ini
Scan for additional .ini files in: (none)
Additional .ini files parsed:      (none)
Copy after login

We have installed two versions of PHP on the above server. As you can see from the above, using the php –ini command can easily locate which configuration file the current PHP command will use.

View class/function/extension information

Usually, we can use the php –info command or use the function phpinfo() in the php program on the web server to display php information, and then Finding information about related classes, extensions or functions is really troublesome.

$ php --info | grep redis
redis
Registered save handlers => files user redis
This program is free software; you can redistribute it and/or modify
Copy after login

We can use the following parameters to view this information more conveniently

--rf       显示关于函数  的信息.
--rc       显示关于类  的信息.
--re       显示关于扩展  的信息.
--rz       显示关于Zend扩展  的信息.
--ri       显示扩展  的配置信息.
Copy after login

For example, we want to view the configuration information of extended redis

$ php --ri redis
 
redis
 
Redis Support => enabled
Redis Version => 2.2.7
Copy after login

View redis class information

$ php --rc redis
Class [  class Redis ] {
 
  - Constants [19] {
    Constant [ integer REDIS_NOT_FOUND ] { 0 }
    ...
  - Methods [201] {
    ...
    Method [  public method echo ] {
    }
    ...
Copy after login

View function printf information

$ php --rf printf
Function [  function printf ] {
 
  - Parameters [2] {
    Parameter #0 [  $format ]
    Parameter #1 [  ...$args ]
  }
}
Copy after login

Syntax check

Sometimes, we only need to check whether the php script exists Syntax errors without executing it, such as checking PHP files for syntax errors in some editors or IDEs.

Use -l (--syntax-check) to perform syntax check only on PHP files.

$ php -l index.php
No syntax errors detected in index.php
Copy after login

If there is a syntax error in our index.php at this time

$ php -l index.php
PHP Parse error:  syntax error, unexpected &#39;echo&#39; (T_ECHO) in index.php on line 3
 
Parse error: syntax error, unexpected &#39;echo&#39; (T_ECHO) in index.php on line 3
Errors parsing index.php
Copy after login

Command line script development

When using PHP to develop command line scripts At the same time, it is obviously different from developing web programs. In web programs, we can provide different inputs for the PHP environment by changing the parameters of the URL. But how to obtain external input in the command line script program?

在使用C语言开发程序时,我们通常会在main函数中提供两个可选的参数int main(int argc, char *argv[]),这两个参数就是从命令行提供的输入参数。在PHP中,提供了两个全局变量$argc和$argv用于获取命令行输入。

  • $argc 包含了 $argv数组包含元素的数目

  • $argv 是一个数组,包含了提供的参数,第一个参数总是脚本文件名称

假设我们有一个名为console.php的命令行脚本文件

<?php echo &#39;命令行参数个数: &#39; . $argc . "n";
echo "命令行参数:n";
foreach ($argv as $index => $arg) {
    echo "    {$index} : {$arg}n";
}
Copy after login

在命令行下执行该脚本

$ php console.php hello world
命令行参数个数: 3
命令行参数:
    0 : console.php
    1 : hello
    2 : world
Copy after login

可以看到,第0个参数是我们执行的脚本名称。需要注意的是,如果提供的第一个参数是以-开头的话,需要在前面增加–,以告诉php这后面的参数是提供给我们的脚本的,而不是php执行文件的(php -r ‘var_dump($argv);’ — -h)。

另外,在脚本中,我们可以通过php_sapi_name()函数判断是否是在命令行下运行的

$ php -r &#39;echo php_sapi_name(), PHP_EOL;&#39;
cli
Copy after login

参考文献

  • Using PHP from the command line

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

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template