Home > Backend Development > PHP Tutorial > Detailed introduction to PHP command line (CLI mode)

Detailed introduction to PHP command line (CLI mode)

不言
Release: 2023-04-04 16:30:02
forward
5432 people have browsed it
This article brings you a detailed introduction to the PHP command line (CLI mode). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

CLI mode

CLI mode is actually the command line running mode, the full English name is Command-Line Interface (Command Line Interface)

$ php -h
Usage: php [options] [-f] <file> [--] [args...]
   php [options] -r <code> [--] [args...]
   php [options] [-B <begin_code>] -R <code> [-E <end_code>] [--] [args...]
   php [options] [-B <begin_code>] -F <file> [-E <end_code>] [--] [args...]
   php [options] -S <addr>:<port> [-t docroot] [router]
   php [options] -- [args...]
   php [options] -a

  -a               Run as interactive shell
                   以交互shell模式运行
  -c <path>|<file> Look for php.ini file in this directory
                   指定php.ini文件所在的目录
  -n               No configuration (ini) files will be used
                   指定不使用php.ini文件
  -d foo[=bar]     Define INI entry foo with value &#39;bar&#39;
                   定义一个INI实体,key为foo,value为&#39;bar&#39;
  -e               Generate extended information for debugger/profiler
                   为调试和分析生成扩展信息
  -f <file>        Parse and execute <file>.
                   解释和执行文件<file>
  -h               This help
                   打印帮助信息
  -i               PHP information
                   显示PHP的基本信息
  -l               Syntax check only (lint)
                   进行语法检查(lint)
  -m               Show compiled in modules
                   显示编译到内核的模块
  -r <code>        Run PHP <code> without using script tags <?..?>
                   运行PHP代码<code>,不需要使用标签<?..?>
  -B <begin_code>  Run PHP <begin_code> before processing input lines
                   在处理输入之前先执行PHP代码<begin_code>
  -R <code>        Run PHP <code> for every input line
                   对输入的每一行作为PHP代码<code>运行
  -F <file>        Parse and execute <file> for every input line
                   对输入的每一行解析和执行<file>
  -E <end_code>    Run PHP <end_code> after processing all input lines
                   在处理所有输入的行之后执行PHP代码<end_code>
  -H               Hide any passed arguments from external tools.
                   隐藏任何来自外部工具传递的参数
  -S <addr>:<port> Run with built-in web server.
                   运行内置的web服务器
  -t <docroot>     Specify document root <docroot> for built-in web server.
                   指定用于内置web服务器的文档根目录<docroot>
  -s               Output HTML syntax highlighted source.
                   输出HTML语法高亮的源码
  -v               Version number
                   输出PHP的版本号
  -w               Output source with stripped comments and whitespace.
                   输出去掉注释和空格的源码
  -z <file>        Load Zend extension <file>.
                   载入Zend扩展文件<file>

  args...          Arguments passed to script. Use -- args when first argument
                   starts with - or script is read from stdin
                   传递给要运行的脚本的参数。当第一个参数以&#39;-&#39;开始或者是脚本是从标准输入读取的时候,使用&#39;--&#39;参数

  --ini            Show configuration file names
                   显示PHP的配置文件名

  --rf <name>      Show information about function <name>.
                   显示关于函数<name>的信息
  --rc <name>      Show information about class <name>.
                   显示关于类<name>的信息
  --re <name>      Show information about extension <name>.
                   显示关于扩展<name>的信息
  --rz <name>      Show information about Zend extension <name>.
                   显示关于Zend扩展<name>的信息
  --ri <name>      Show configuration for extension <name>.
                   显示扩展<name>的配置信息
Copy after login

Run PHP in interactive shell mode

http://php.net/manual/en/features.commandline.interactive.php
The interactive shell stores your history which can be accessed using the up and down keys. The history is saved in the ~/.php_history file.
The interactive shell mode saves the entered historical commands and can be accessed using the up and down keys. . The history is saved in the ~/.php_history file.

$ php -a
Interactive shell

php > echo 5+8;
php > function addTwo($n)
php > {
php { return $n + 2;
php { }
php > var_dump(addtwo(2));
int(4)
Copy after login

Find information about related classes, extensions or functions

Usually, we can use the php --info command or on the web server In the PHP program, the function phpinfo() is used to display PHP information, and then to find information about related classes, extensions or functions. This 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

Grammar check

You only need to check whether the php script has syntax errors, without executing it, such as in some editors or IDEs Check the PHP file for syntax errors.

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 index.php.

$ 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

$argc contains the number of elements contained in the $argv array
$argv is an array that contains The parameters provided, the first parameter is always the command line script file of the script file name
console.php

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


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

can be seen, the 0th The parameter is the name of the script we execute. It should be noted that if the first parameter provided starts with -, you need to add - in front to tell PHP that the following parameters are provided to our script, not to the PHP execution file (php -r 'var_dump($argv);' -- -h).
In addition, in the script, we can use the php_sapi_name() function to determine whether it is running under the command line.

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

The above is the detailed content of Detailed introduction to PHP command line (CLI mode). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
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