Drupal

命令行环境下PHP常用参数解析

2023-02-06  本文已影响0人  rightchen

整理一下命令行下的PHP参数使用。输入php -h ,查看帮助,可以调出PHP官方提供的参数说明。

# 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
  -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命令回车后立即执行,执行完后等待下一条命令键入,exit退出,多条命令可用 分号 间隔,方向键上和下可以调出历史命令,平时需要临时调试一个 正则表达式 或当个计算器非常实用。

# php -a
Interactive shell

php > echo "hello world";
hello world
php > echo 100+100;
200
php > exit

php -c

指定 php.ini 配置文件所在的目录或指定一个php配置文件,如:

php -c ./php.ini test.php

 # php -c /home/htf/my_php.ini

php -n

忽略php.ini配置文件执行

php -d

用bar代替配置文件中foo的值

php -f <file>

解析并执行一个php脚本文件,默认执行时以有这个参数

php -h

查看命令行下PHP参数的信息说明

php -i

查看php的信息。命令行下的phpinfo参数

# php -i|grep redis
/usr/local/etc/php/conf.d/docker-php-ext-redis.ini,
redis
Registered save handlers => files user redis rediscluster 
This program is free software; you can redistribute it and/or modify

php -l <file>

检查文件的语法信息。例如有check_syntax.php

<?php
echo hello world"
?>
# php -l check_syntax.php 

Parse error: syntax error, unexpected 'world' (T_STRING), expecting ',' or ';' in check_syntax.php on line 2
Errors parsing check_syntax.php

php -m

显示命令行环境下加载的PHP模块

php -r <script>

执行PHP代码

# php -r "echo 'hello world'.PHP_EOL;"
hello world

php -s <file>

输出代码高亮的html格式内容。

php -v

显示php的版本信息

php -w <file>

将源代码中的内容去除注释和空白行后进行展示

# cat check_syntax.php 
<?php
//php say hello

echo "hello world";
//finish
?>

# php -w check_syntax.php 
<?php
 echo "hello world"; ?>


上一篇下一篇

猜你喜欢

热点阅读