我希望能够在命令行上运行一行PHP代码,类似于以下选项的工作方式:
perl -e "print 'hi';"
python -c "print 'hi'"
ruby -e "puts 'hi'"
我希望能够做到:
php "echo 'hi';"
我读到有一个-r
选项可以满足我对php的需求,但是当我try 使用它时,它似乎不可用.我try 过使用PHP5.2.13和PHP4.4.9,但都没有可用的-r
选项.
我编写了这个脚本(我称之为run_php.php)-它可以工作,但我并不是它的狂热粉丝,只是因为我觉得应该有一种更"正确"的方式来做这件事.
#!/usr/bin/php5 -q
<?php echo eval($argv[1]); ?>
有-r
个选项吗?如果是这样,为什么在我运行--help
时它不可用?如果没有-r
选项,那么最好的方法是什么(如果可能的话,不需要编写中间脚本)?
因为我不认为上面说得很清楚,-r
选项对我来说是not.下面是我正在运行的两个PHP版本的php -h
输出.
PHP 4.4.9
Usage: php [-q] [-h] [-s] [-v] [-i] [-f <file>]
php <file> [args...]
-a Run interactively
-C Do not chdir to the script's directory
-c <path>|<file> Look for php.ini file in this directory
-n No php.ini file will be used
-d foo[=bar] Define INI entry foo with value 'bar'
-e Generate extended information for debugger/profiler
-f <file> Parse <file>. Implies `-q'
-h This help
-i PHP information
-l Syntax check only (lint)
-m Show compiled in modules
-q Quiet-mode. Suppress HTTP Header output.
-s Display colour syntax highlighted source.
-v Version number
-w Display source with stripped comments and whitespace.
-z <file> Load Zend extension <file>.
PHP 5.2.13
Usage: php [-q] [-h] [-s] [-v] [-i] [-f <file>]
php <file> [args...]
-a Run interactively
-C Do not chdir to the script's directory
-c <path>|<file> Look for php.ini file in this directory
-n No php.ini file will be used
-d foo[=bar] Define INI entry foo with value 'bar'
-e Generate extended information for debugger/profiler
-f <file> Parse <file>. Implies `-q'
-h This help
-i PHP information
-l Syntax check only (lint)
-m Show compiled in modules
-q Quiet-mode. Suppress HTTP Header output.
-s Display colour syntax highlighted source.
-v Version number
-w Display source with stripped comments and whitespace.
-z <file> Load Zend extension <file>.
有no-r选项.当我try 使用-r
选项时,我得到:
参数1中的字符2出错:找不到选项r
抱歉搞混了.