首先要保证php在cli模式下可用,php –v会返回PHP的版本号。

[gaojian3@log001 ~]$ php -v
PHP 5.2.14 (cli) (built: Aug 31 2012 15:03:20)
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2010 Zend Technologies

 

一、命令行自定义变量

      在命令行里输入程序参数来更改其运行方式是很常见的做法。你也可以对CLI程序这样做。PHP CLI带有两个特殊的变量,专门用来达到这个目的:

1、$argv变量,它通过命令行把传递给PHP脚本的参数保存为单独的数组元素(v:value)

2、$argc变量,它用来保存$argv数组里元素的个数(c : count)

test.php脚本:

<?php
print_r ($argv);
var_dump ($argc);
?>
[gaojian3@log001 ~]$ php /usr/home/gaojian3/test.php 56787 98989 0000
Array
(
[0] => /usr/home/gaojian3/test.php
[1] => 56787
[2] => 98989
[3] => 0000
)
int(4)

正如你可以从输出的结果看到的,传递给test.php的值会自动地作为数组元素出现在$argv里。要注意的是,$argv的第一个自变量总是脚本自己的名称。使用:采用 $argv[3] 这种形式来获取参数。

 

二、命令行变量

除了用命令行传递PHP脚本参数,还可以传递PHP CLI参数以更改其工作方式。

[gaojian3@log001 ~]$ man php
NAME
php - PHP Command Line Interface ’CLI’ SYNOPSIS
php [options] [ -f ] file [[--] args...] php [options] -r code [[--] args...] php [options] [-B code] -R code [-E code] [[--] args...] php [options] [-B code] -F file [-E code] [[--] args...] php [options] -- [ args...] php [options] -a OPTIONS
--interactive
-a Run PHP interactively. This lets you enter snippets of PHP code that directly get executed. When readline support is
enabled you can edit the lines and also have history support. --php-ini path|file
-c path|file Look for php.ini file in the directory path or use the specified file --no-php-ini
-n No php.ini file will be used --define foo[=bar]
-d foo[=bar] Define INI entry foo with value bar -e Generate extended information for debugger/profiler --file file
-f file Parse and execute file --global name
-g name Make variable name global in script. --help
-h This help --hide-args
-H Hide script name (file) and parameters (args...) from external tools. For example you may want to use this when a php
script is started as a daemon and the command line contains sensitive data such as passwords. --info
-i PHP information and configuration --syntax-check
-l Syntax check only (lint) --modules
-m Show compiled in modules --run code
-r code Run PHP code without using script tags ’<?..?>’ --process-begin code
-B code Run PHP code before processing input lines --process-code code
-R code Run PHP code for every input line --process-file file --modules
-m Show compiled in modules --run code
-r code Run PHP code without using script tags ’<?..?>’ --process-begin code
-B code Run PHP code before processing input lines --process-code code
-R code Run PHP code for every input line --process-file file
-F file Parse and execute file for every input line --process-end code
-E code Run PHP code after processing all input lines --syntax-highlight
-s Display colour syntax highlighted source --version
-v Version number --stripped
-w Display source with stripped comments and whitespace --zend-extension file
-z file Load Zend extension file args... Arguments passed to script. Use ’--’ args when first argument starts with ’-’ or script is read from stdin --rfunction name
--rf name Shows information about function name --rclass name
--rc name Shows information about class name --rextension name
--re name Shows information about extension name

实际上,本文一开始参数传递部分只是php cli模式下的一小部分,之所以单独摘出来,是因为参数传递最常用,也是平时用得最多的部分。

 

最好玩的部分是php –a,php的交互模式,之前没发现,现在发现真心很好用。其余的还有好多,大家都试下。

[gaojian3@log001 ~]$ php -a
Interactive mode enabled <?php
echo 97678%32;
14
echo "my code";
my code

PHP命令行模式基本介绍的更多相关文章

  1. 好压(HaoZip)的命令行模式用法介绍

    好压压缩软件,又叫“2345好压”,是一款国产的优秀压缩软件,目前是免费的,据官网介绍,该软件永久免费.官网地址:http://haozip.2345.com/ 本文主要对该软件的命令行模式用法进行介 ...

  2. WinRAR的命令行模式用法介绍

    因工作中要对数据打包,顺便研究了下WinRAR的命令行模式,自己写了些例子,基本用法如下: 测试压缩文件准备:文件夹test_data,内部包含子文件夹,分别存放了一些*.log和*.txt文件. 测 ...

  3. 命令行模式下 MYSQL导入导出.sql文件的方法

    一.MYSQL的命令行模式的设置:桌面->我的电脑->属性->环境变量->新建->PATH=“:path\mysql\bin;”其中path为MYSQL的安装路径.二.简 ...

  4. 《Practical Vim》第五章:命令行模式

    前言 出于历史原因,命令行模式又叫 Ex 模式,它具有的功能有: 读写文件:比如 :edit, :write 等等 创建标签页或者窗口:比如 :tabnew :split 操作缓存区,比如: bnex ...

  5. Linux(CentOS 7)命令行模式安装VMware Tools 详解

    本篇文章主要介绍了如何在Linux(CentOS 7)命令行模式安装VMware Tools,具有一定的参考价值,感兴趣的小伙伴们可以参考一下. 本例中为在Linux(以CentOS 7为例)安装VM ...

  6. jmeter命令行模式运行,实时获取压测结果

    jmeter命令行模式运行,实时获取压测结果 jmeter很小,很快,使用方便,可以在界面运行,可以命令行运行.简单介绍下命令行运行的方式: sh jmeter.sh -n -t my-script. ...

  7. PHP 命令行模式实战之cli+mysql 模拟队列批量发送邮件(在Linux环境下PHP 异步执行脚本发送事件通知消息实际案例)

    源码地址:https://github.com/Tinywan/PHP_Experience 测试环境配置: 环境:Windows 7系统 .PHP7.0.Apache服务器 PHP框架:ThinkP ...

  8. Jmeter使用非 GUI 模式,即命令行模式运行实例讲

    转载:http://www.cnblogs.com/leeboke/p/5238269.html 参考资料:https://girliemangalo.wordpress.com/2009/10/29 ...

  9. 命令行模式运行jmeter,主从方式运行jmeter

    jmeter很小,很快,使用方便,可以在界面运行,可以命令行运行.简单介绍下命令行运行的方式: sh jmeter.sh -n -t my-script.jmx -R 10.6.5.31,10.6.5 ...

随机推荐

  1. 取消界面的title

    在setContentView(R.layout.activity_main)方法上面添加代码(继承Activity的写法): requestWindowFeature(Window.FEATURE_ ...

  2. STM32F0xx_EXIT中断配置详细过程

    Ⅰ.概述 EXIT外部中断在使用到按键或者开关控制等应用中比较常见,低功耗中断唤醒也是很常见的一种.因此,EXIT在实际项目开发中也是比较常见的一种. STM32F0中外部中断EXIT属于中断和事件的 ...

  3. UART,USART,SPI,I2C等总线的介绍与区别20160526

    首先来说一下UART和USART的区别: 1.字面意义: UART:universal asynchronous receiver and transmitter通用异步收发器: USART:univ ...

  4. jruby中的异常

    先看看ruby中的异常知识: 异常处理raise 例子: raise raise "you lose" raise SyntaxError.new("invalid sy ...

  5. word中让首页和目录不显示页码的方法

    在正文前一页,插入->分隔符->下一页,然后插入页码,取消与前一页页眉的链接,删除首页和目录的页码即可

  6. 做HDU1010 带出来一个小问题

    做1010  本来是想的DFS深搜  但是自己凭空打  打不出来  因为没有记模板  然后就去搜  但是看了一遍  自己打却又是有BUG  然后验证  就出现了一个二维字符数组打印的问题 开始代码是这 ...

  7. 转 在SQL Server中创建用户角色及授权(使用SQL语句)

     目录 要想成功访问 SQL Server 数据库中的数据 我们需要两个方面的授权 完整的代码示例 使用存储过程来完成用户创建 实例 要想成功访问 SQL Server 数据库中的数据, 我们需要两个 ...

  8. 在Java如何保证方法是线程安全的

    废话开篇 都说Java程序好些,但是我觉得Java编程这东西,没个十年八年的真不敢说自己精通Java编程,由于工作原因,开始转战Java多线程,以前没怎么接触过,所以想留点脚印在这两条路上. 切入正题 ...

  9. [译]rabbitmq 2.1 Consumers and producers (not an economics lesson)

    我对rabbitmq学习还不深入,这些翻译仅仅做资料保存,希望不要误导大家. For now, all you need to know is that producers create messag ...

  10. Swift 中的利刃,函数和闭包

    input[type="date"].form-control,.input-group-sm>input[type="date"].input-grou ...