我希望能够在命令行上运行一行 PHP 代码,类似于以下选项的工作方式:
perl -e "print 'hi';"
python -c "print 'hi'"
ruby -e "puts 'hi'"
我希望能够做到:
php "echo 'hi';"
我读到有一个 -r选项,可以做我所需要的 php,但它似乎不可用时,我试图使用它。我试过使用 PHP 5.2.13和 PHP 4.4.9,但都没有可用的 -r选项。
我编写了这个脚本(我称之为 run _ php.php)——它可以工作,但我并不是它的超级粉丝,因为我觉得应该有一种更“正确”的方式来完成它。
#!/usr/bin/php5 -q
<?php echo eval($argv[1]); ?>
有 -r选项吗?如果是这样,为什么当我运行 --help时它不可用?如果没有 -r选项,那么做到这一点的最佳方法是什么(如果可能的话,不需要编写中间脚本) ?
因为我不认为它是非常清楚以上,-r选项是 没有可用于我。下面是我正在运行的两个版本的 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>.
有 没有-r 选项。当我尝试使用 -r选项时,我得到:
参数1错误,char 2: 选项未找到 r
抱歉给你添麻烦了。