PHP 命令行 Commands

| 选择喜欢的代码风格  

PHP 命令解析


本文采用的 php 版本如下:

[root@dsp-adx /]$ php -v
./php -v
PHP 7.4.7 (cli) (built: Jul  3 2020 09:55:07) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies

PHP 命令列表


执行 php -hphp --help 获取所有命令,如下:

// 使用的格式 有些命令需要搭配使用

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
  -c <path>|<file> Look for php.ini file in this directory
  -n               No configuration (ini) files will be used
  -d foo[=bar]     Define INI entry foo with value 'bar'
  -e               Generate extended information for debugger/profiler
  -f <file>        Parse and execute <file>.
  -h               This help
  -i               PHP information
  -l               Syntax check only (lint)
  -m               Show compiled in modules
  -r <code>        Run PHP <code> without using script tags <?..?>
  -B <begin_code>  Run PHP <begin_code> before processing input lines
  -R <code>        Run PHP <code> for every input line
  -F <file>        Parse and execute <file> for every input line
  -E <end_code>    Run PHP <end_code> after processing all input lines
  -H               Hide any passed arguments from external tools.
  -S <addr>:<port> Run with built-in web server.
  -t <docroot>     Specify document root <docroot> for built-in web server.
  -s               Output HTML syntax highlighted source.
  -v               Version number
  -w               Output source with stripped comments and whitespace.
  -z <file>        Load Zend extension <file>.

  args...          Arguments passed to script. Use -- args when first argument
                   starts with - or script is read from stdin

  --ini            Show configuration file names

  --rf <name>      Show information about function <name>.
  --rc <name>      Show information about class <name>.
  --re <name>      Show information about extension <name>.
  --rz <name>      Show information about Zend extension <name>.
  --ri <name>      Show configuration for extension <name>.

php -a


交互式运行 PHP:

[root@dsp-adx /]
$ php -a
Interactive shell

php > $a = 5;
php > $b = 6;
php > echo $a*$b;
30
php > 
php > echo time();
1567499373
php > 
php > 
php > ^C

php -d


设置 ini 配置的键值:

[root@dsp-adx /]
$ php -i | grep opcache.enable_cli
opcache.enable_cli => Off  => Off 

-------------
php -d 'opcache.enable_cli=1' -i | grep opcache.enable_cli
opcache.enable_cli => On => On

php --rf


输出一个函数的信息:

[root@dsp-adx /]
$ php --rf date
Function [ <internal:date> function date ] {

  - Parameters [2] {
    Parameter #0 [ <required> $format ]
    Parameter #1 [ <optional> $timestamp ]
  }
}

php --ri


输出一个扩展的配置信息:

[root@dsp-adx /]
$ php --ri swoole

swoole

Swoole => enabled
Author => Swoole Team <team@swoole.com>
Version => 4.4.4
Built => Aug 29 2019 10:42:41
coroutine => enabled
epoll => enabled
eventfd => enabled
signalfd => enabled
cpu_affinity => enabled
spinlock => enabled
rwlock => enabled
http2 => enabled
pcre => enabled
zlib => enabled
mutex_timedlock => enabled
pthread_barrier => enabled
futex => enabled
async_redis => enabled

Directive => Local Value => Master Value
swoole.enable_coroutine => On => On
swoole.enable_library => On => On
swoole.enable_preemptive_scheduler => Off => Off
swoole.display_errors => On => On
swoole.use_shortname => On => On
swoole.unixsock_buffer_size => 8388608 => 8388608

php -s


输出脚本文件源码为 html,并且语法高亮:

[root@dsp-adx /]
$ php -s CommandNotFound.php 
<code><span style="color: #000000">
<span style="color: #0000BB"><?php<br /><br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">time</span><span style="color: #007700">();<br /></span>
</span>
</code>

php -w


去除脚本文件源码的注释和空格,然后输出:

[root@dsp-adx /]
$ php -w CommandNotFound.php 
<?php
 echo time(); 

php -S & php -t


php -Sphp -t 经常搭配使用:

[root@dsp-adx /]
$ php -S localhost:9998 -t ./
PHP 7.3.7 Development Server started at Tue Sep  3 16:41:40 2019
Listening on http://localhost:9998
Document root is /home/www/CommandNotFound
Press Ctrl-C to quit.


[root@dsp-adx /]
$ php -S localhost:9998 -t /
PHP 7.3.7 Development Server started at Tue Sep  3 16:42:20 2019
Listening on http://localhost:9998
Document root is /
Press Ctrl-C to quit.

– ARGS


php 在用户自定义函数中支持可变数量的参数列表。注意:在 PHP 5.5 及更早的版本中,使用 func_num_args()func_get_arg()func_get_args() 函数实现,在 php 5.6 及以上的版本中,可以使用 语法实现。

[root@dsp-adx /]
$ php -f CommandNotFound.php -- php 7 command not found
array(6) {
  [0]=>
  string(19) "CommandNotFound.php"
  [1]=>
  string(3) "php"
  [2]=>
  string(1) "7"
  [3]=>
  string(7) "command"
  [4]=>
  string(3) "not"
  [5]=>
  string(5) "found"
}

PHP –INI


php --ini
Configuration File (php.ini) Path: /Data/apps/php7.3.7/etc
Loaded Configuration File:         /Data/apps/php7.3.7/etc/php.ini
Scan for additional .ini files in: (none)
Additional .ini files parsed:      (none)

最后,PHPer 常用的命令


php -S 
php -m
php -i
php -l
php -r
php --ini
php -- args

PHP 命令行扩展阅读:




发表评论