PHP命令行(CLI模式)
CLI模式
CLI模式其实就是命令行运行模式,英文全称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 'bar'
定义一个INI实体,key为foo,value为'bar'
-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
传递给要运行的脚本的参数。当第一个参数以'-'开始或者是脚本是从标准输入读取的时候,使用'--'参数
--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>的配置信息
以交互式Shell模式运行PHP
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.
交互shell模式保存输入的历史命令,可以使用上下键访问到。历史被保存在~/.php_history文件。
$ php -a
Interactive shell
php > echo 5+8;
13
php > function addTwo($n)
php > {
php { return $n + 2;
php { }
php > var_dump(addtwo(2));
int(4)
查找相关类、扩展或者函数的信息
通常,我们可以使用php --info命令或者在在web服务器上的php程序中使用函数phpinfo()显示php的信息,然后再查找相关类、扩展或者函数的信息,这样做实在是麻烦了一些。
$ php --info | grep redis redis Registered save handlers => files user redis This program is free software; you can redistribute it and/or modify
语法检查
只需要检查php脚本是否存在语法错误,而不需要执行它,比如在一些编辑器或者IDE中检查PHP文件是否存在语法错误。
使用-l(--syntax-check)可以只对PHP文件进行语法检查。
$ php -l index.php No syntax errors detected in index.php
假如index.php中存在语法错误。
$ php -l index.php PHP Parse error: syntax error, unexpected 'echo' (T_ECHO) in index.php on line 3 Parse error: syntax error, unexpected 'echo' (T_ECHO) in index.php on line 3 Errors parsing index.php
命令行脚本
$argc 包含了 $argv数组包含元素的数目
$argv 是一个数组,包含了提供的参数,第一个参数总是脚本文件名称
console.php的命令行脚本文件
<?php
echo '命令行参数个数: ' . $argc . "\n";
echo "命令行参数:\n";
foreach ($argv as $index => $arg) {
echo " {$index} : {$arg}\n";
}
$ php console.php hello world
命令行参数个数: 3
命令行参数:
0 : console.php
1 : hello
2 : world
可以看到,第0个参数是我们执行的脚本名称。需要注意的是,如果提供的第一个参数是以-开头的话,需要在前面增加--,以告诉php这后面的参数是提供给我们的脚本的,而不是php执行文件的(php -r 'var_dump($argv);' -- -h)。
另外,在脚本中,我们可以通过php_sapi_name()函数判断是否是在命令行下运行的。
$ php -r 'echo php_sapi_name(), PHP_EOL;' cli
PHP命令行(CLI模式)的更多相关文章
- PHP的CLI命令行运行模式浅析
在做开发的时候,我们不仅仅只是做各种网站或者接口,也经常需要写一些命令行脚本用来处理一些后端的事务.比如对数据进行处理统计等.当然也是为了效率着想,当一个事务有可能会有较长的耗时时,往往会交由服务器的 ...
- Apache Commons CLI官方文档翻译 —— 快速构建命令行启动模式
昨天通过几个小程序以及Hangout源码学习了CLI的基本使用,今天就来尝试翻译一下CLI的官方使用手册. 下面将会通过几个部分简单的介绍CLI在应用中的使用场景. 昨天已经联系过几个基本的命令行参数 ...
- NET Core 环境搭建和命令行CLI入门
NET Core 环境搭建和命令行CLI入门 2016年6月27日.NET Core & ASP.NET Core 1.0在Redhat峰会上正式发布,社区里涌现了很多文章,我也计划写个系列文 ...
- NET Core 环境搭建和命令行CLI入门[转]
NET Core 环境搭建和命令行CLI入门 时间:2016-07-06 01:48:19 阅读:258 评论:0 收藏:0 [点我收藏+] 标签: N ...
- Jmeter在非GUI(命令行)模式下生成测试报告
根据各大招聘网站上的需求来看,熟悉Jmeter做性能测试已经几乎成为必要条件了. 那么今天在这个给大家安利一波,怎么使用Jmeter在非GUI(命令行)模式下生成测试报告呢?? 条件准备: 1.Jme ...
- Appium命令行工作模式
前面如何快速搭建基于python+appium的自动化测试环境介绍过安装Appium-desktop的客户端版本,然后每次需要运行脚本的时候都要先去找到Appium应用并双击打开,再点击Start S ...
- .NET Core系列 : 1、.NET Core 环境搭建和命令行CLI入门
2016年6月27日.NET Core & ASP.NET Core 1.0在Redhat峰会上正式发布,社区里涌现了很多文章,我也计划写个系列文章,原因是.NET Core的入门门槛相当高, ...
- jenkins 命令行 CLI jenkins-cli.jar
部署好jenkins后,一般都是通过jenkins提供的web界面来操作jenkins. 而有些场景则需要通过命令来操作jenkins,例如通过脚本操作jenkins. 在jenkins提供的web界 ...
- Python 命令行(CLI)基础库
在 CLI 下写 UI 应用 前阵子看了一下自己去年写的 Python-视频转字符动画,感觉好糗..所以几乎把整篇文章重写了一遍.并使用 curses 库实现字符动画的播放. 但是感觉,curses ...
- Centos7更改默认启动桌面(或命令行)模式
centos7以后是这样的,7以前就是别的版本了 1.systemctl get-default命令获取当前模式 2.systemctl set-default graphical.target 修改 ...
随机推荐
- 一个C#面试问题,要求是将字符串中重复字符从第二个开始都去掉,空格除外。然后显示的时候,从后往前显示。
因为C#的code,感觉实现这个还是比较容易,只是SB.所以,在面试时候,以为不会这么容易,所以,我先试着用了Dictionary去实现,发现有困难.然后改回来用StringBuilder实现,这个因 ...
- SQLServer存储过程 实例,很多语法可以以后参考
SQL代码 alter PROCEDURE sp_addnewdtgtype ( ), @dtgdllcontent image, ) ) AS BEGIN ); declare @v_count i ...
- 使用 SQL SERVER PROFILER 监测死锁
作为DBA,可能经常会遇到有同事或者客户反映经常发生死锁,影响了系统的使用.此时,你需要尽快侦测和处理这类问题. 死锁是当两个或者以上的事务互相阻塞引起的.在这种情况下两个事务会无限期地等待对方释放资 ...
- /etc/hosts.allow和/etc/hosts.deny详解
今天遇到一台服务器22端口正常,但是通过ssh连接的问题.排查了防火墙和端口问题,半天没有找出来原因,后来求助大神,终于明白了通过etc目录下hosts.deny和hosts.allow文件可以限制远 ...
- python:协程
1,如何实现在两个函数之间的切换? def func1(): print(l) yield print(3) yield def func2(): g =func1() next(g) print(2 ...
- POJ 1423 斯特林
题意:进制问题 分析: 打表,但是要用不能 long long 型,超内存. n! = log_{10}\sqrt{2{\pi}n}*(\frac{n}e)^n 精度要求 #include <c ...
- 小练习——关于循环条件---for
1.打印100个“非常”的句子 static void Main(string[] args) { //打印20个“非常" Console.WriteLine("打印100个非常& ...
- HDU 2586 How far away ?【LCA】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=2586 How far away ? Time Limit: 2000/1000 MS (Java/Oth ...
- POJ 3764 The xor-longest Path 【01字典树&&求路径最大异或和&&YY】
题目传送门:http://poj.org/problem?id=3764 The xor-longest Path Time Limit: 2000MS Memory Limit: 65536K ...
- Spring多个版本源码地址分享
源码地址为:http://repo.spring.io/simple/libs-release-local/org/springframework/spring/,以供研究源码的朋友. 我看了好几本关 ...