找回密码
 新建账号

php -a run as interactive shell 无浏览器无文件运行php执行代码

[复制链接]
php 发表于 2016/9/17 21:54 | 显示全部楼层 |阅读模式
php cli the interactive CLI shell 命令行,可以不借助浏览器来运行PHP代码,也不需要像php -f一样创建一个php文件。
php -a run as interactive shell进入交互模式输入并运行PHP代码,进行交互模式以后,每次输入代码以后回车,PHP会执行到回车前的代码,如果代码中有输出,则自动显示输出,执行完毕后可以继续输入代码,之前的代码仍然生效。
需要退出交互模式时,可以按ctrl+c或者输入quit后回车。
注:Windows PHP 7.1+ 才支持交互模式,输入代码时不需要加<?php和?>,PHP 7.1+是通过the WinEditLine library实现readline extension的。
对于Windows PHP 7.0以及更早版本,可以直接在命令行窗口输入php回车,不必加-a,然后输入PHP代码,按回车以后,按ctrl+z然后再按一次回车,代码执行以后php会自动显示最终输出内容并且退出运行,需要注意的是PHP代码,需要加<?php,而?>可以不加。
运行php cli在命令行窗口中输入php.exe,也可以省略.exe,直接输入php,如果已经将php.exe所在目录添加到了PATH变更,则可以直接调用,如果没有修改环境变量PATH,则需要先通过cd /d "目录路径"将工作路径切换到php主目录。

D:\Programs\PHPServer\Bin\PHP.CGI\PHP7.1>php -a
Interactive shell
php > $url = 'http://www.wuxiancheng.cn/';
php > echo $url;
http://www.wuxiancheng.cn/

D:\Programs\PHPServer\Bin\PHP.CGI\PHP5.4>php -n -a
Interactive mode enabled
<?php
        echo mt_rand();
?>
^Z
1799987106
D:\Programs\PHPServer\Bin\PHP.CGI\PHP5.4>

php命令行常用指令用法释义:
-a 进入交互模式
-c 指定php.ini配置文件的路径或者所在目录的路径,扩展名可以不是.ini
-n 不指定php.ini,使用php默认配置
-d 定义php.ini配置项,可以使用多次,如 -d display_errors = 1
-f 指定要运行的php脚本文件
-r 直接运行PHP代码,不需要加<?php和?>,代码一定要跟在-r后面,如 php -n -r "echo 'hello';"
-S 直接将php作为web服务器使用,语法是 php -S www.wuxiancheng.cn:8080 -t %cd%,域名也可以换成IP,-t是指网站根目录
-v 显示版本号
-h 或者 --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]
   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>.

手机版|轻松E站

GMT+8, 2024/3/29 09:07

快速回复 返回顶部 返回列表