php 命令详解

| 选择喜欢的代码风格  

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

php 命令安装:


-bash/zsh: php: command not found

# Windows (WSL2)
sudo apt-get update sudo apt-get install php

# Debian
apt-get install php

# Ubuntu
apt-get install php

# Alpine
apk add php

# Arch Linux
pacman -S php

# Kali Linux
apt-get install php

# CentOS
yum install php-cli

# Fedora
dnf install php-cli

# OS X
brew install php

# Raspbian
apt-get install php

# Dockerfile
dockerfile.run/php

# Docker
docker run cmd.cat/php php

php 命令补充说明:


php 命令可以以命令行的方式运行,非常灵活:支持交互式 Shell,CLI SAPI 有三种不同的方法执行 PHP 代码:

# 1. 运行指定 php 文件

php my_script.php
php -f my_script.php

# 2. 在命令行中直接传递 PHP 代码执行
php -r 'print_r(get_defined_constants());'


# 3. 通过标准输入(stdin)提供需要运行的 PHP 代码
some_application | some_filter | php | sort -u > final_output.txt

php 命令语法:


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

php 命令选项:


  -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 命令实例:


php 命令解析并执行 PHP 脚本:

php path/to/*.php

php 命令检查 PHP 脚本的语法(即 Lint):

php -l path/to/file

交互式运行 PHP:

php -a

==========================================
Interactive shell

php > echo 2+3;
5
php > echo 9-6;
3
php > echo 5*4;
20
php > echo 12/3;
4
php > echo 12/5;
2.4
php > echo 2+3-1;
4
php > echo 2+3-1*3;
2
php > exit

运行 PHP 代码 - 无需使用 <? ?> 标签;用反斜杠转义双引号

php -r "code"
php -r 'phpinfo();'

==========================================
PHP Version => 7.3.7

System => Linux dsp-2 3.10.0-1127.8.2.el7.x86_64 #1 SMP Tue May 12 16:57:42 UTC 2020 x86_64
Build Date => Jul 12 2019 18:28:35
Configure Command =>  './configure'  '--prefix=/Data/apps/php7.3.7' '--with-config-file-path=/Data/apps/php7.3.7/etc' '--enable-fpm' '--with-fpm-user=nginx' '--with-fpm-group=nginx' '--enable-inline-optimization' '--disable-debug' '--disable-rpath' '--enable-shared' '--enable-soap' '--with-libxml-dir' '--with-xmlrpc' '--with-openssl' '--with-pcre-regex' '--with-sqlite3' '--with-zlib' '--enable-bcmath' '--with-iconv' '--with-bz2' '--enable-calendar' '--with-curl' '--with-cdb' '--enable-dom' '--enable-exif' '--enable-fileinfo' '--enable-filter' '--with-pcre-dir' '--enable-ftp' '--with-gd' '--with-openssl-dir' '--with-jpeg-dir' '--with-png-dir' '--with-zlib-dir' '--with-freetype-dir' '--enable-gd-jis-conv' '--with-gettext' '--with-gmp' '--with-mhash' '--enable-json' '--enable-mbstring' '--enable-mbregex' '--enable-mbregex-backtrack' '--with-onig' '--enable-pdo' '--with-mysqli=mysqlnd' '--with-pdo-mysql=mysqlnd' '--with-pdo-sqlite' '--with-readline' '--enable-session' '--enable-shmop' '--enable-simplexml' '--enable-sockets' '--enable-sysvmsg' '--enable-sysvsem' '--enable-sysvshm' '--enable-wddx' '--with-xsl' '--enable-zip' '--enable-mysqlnd-compression-support' '--with-pear' '--enable-opcache'
Server API => Command Line Interface
Virtual Directory Support => disabled
Configuration File (php.ini) Path => /Data/apps/php7.3.7/etc
Loaded Configuration File => /Data/apps/php7.3.7/etc/php.ini
Scan this dir for additional .ini files => (none)
Additional .ini files parsed => (none)
PHP API => 20180731
PHP Extension => 20180731
Zend Extension => 320180731
Zend Extension Build => API320180731,NTS
PHP Extension Build => API20180731,NTS
Debug Build => no
Thread Safety => disabled
Zend Signal Handling => enabled
Zend Memory Manager => enabled
Zend Multibyte Support => provided by mbstring
IPv6 Support => enabled
DTrace Support => disabled

Registered PHP Streams => https, ftps, compress.zlib, compress.bzip2, php, file, glob, data, http, ftp, phar, zip
Registered Stream Socket Transports => tcp, udp, unix, udg, ssl, sslv3, tls, tlsv1.0, tlsv1.1, tlsv1.2
Registered Stream Filters => zlib.*, bzip2.*, convert.iconv.*, string.rot13, string.toupper, string.tolower, string.strip_tags, convert.*, consumed, dechunk

This program makes use of the Zend Scripting Language Engine:
Zend Engine v3.3.7, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.3.7, Copyright (c) 1999-2018, by Zend Technologies

_______________________________________________________________________

....
php -r 'print_r(get_defined_constants());'

==========================================
Array
(
    [E_ERROR] => 1
    [E_RECOVERABLE_ERROR] => 4096
    [E_WARNING] => 2
    [E_PARSE] => 4
    [E_NOTICE] => 8
    [E_STRICT] => 2048
    [E_DEPRECATED] => 8192
    [E_CORE_ERROR] => 16
    [E_CORE_WARNING] => 32
    [E_COMPILE_ERROR] => 64
    [E_COMPILE_WARNING] => 128
    [E_USER_ERROR] => 256
    [E_USER_WARNING] => 512
    [E_USER_NOTICE] => 1024
    [E_USER_DEPRECATED] => 16384
    [E_ALL] => 32767
    [DEBUG_BACKTRACE_PROVIDE_OBJECT] => 1
    [DEBUG_BACKTRACE_IGNORE_ARGS] => 2
    [TRUE] => 1
    [FALSE] => 
    [ZEND_THREAD_SAFE] => 
    [ZEND_DEBUG_BUILD] => 
    [NULL] => 
    [PHP_VERSION] => 7.3.7
    [PHP_MAJOR_VERSION] => 7
    [PHP_MINOR_VERSION] => 3
    [PHP_RELEASE_VERSION] => 7

...

在当前目录中启动 PHP 内置 Web 服务器:

php -S host:port

==========================================
php -S 127.0.0.1:8080

获取已安装的 PHP 扩展的列表:

php -m

显示有关当前 PHP 配置的信息:

php -i

php 显示有关特定功能的信息:

php --rf function_name

==========================================
php --rf time
Function [  function time ] {

  - Parameters [0] {
  }
}

php 扩展阅读:




php 命令评论