PHP命令行模式基本介绍

时间:2023-11-15 18:03:08

 

首先要保证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