在命令行上执行一串 PHP 代码
2022-08-30 08:33:21
我希望能够在命令行上运行一行PHP代码,类似于以下选项的工作方式:
perl -e "print 'hi';"
python -c "print 'hi'"
ruby -e "puts 'hi'"
我希望能够做到:
php "echo 'hi';"
我已经读到有一个选项可以做我需要的php,但是当我尝试使用它时,它似乎不可用。我尝试过使用PHP 5.2.13和PHP 4.4.9,但都没有可用的选项。-r
-r
我写了这个脚本(我称之为run_php.php) - 它有效,但我不是它的忠实粉丝,只是因为我觉得应该有一个更“正确”的方式来做到这一点。
#!/usr/bin/php5 -q
<?php echo eval($argv[1]); ?>
有选择吗?如果是这样,为什么在我运行时它不可用?如果没有选项,最好的方法是什么(如果可能的话,无需编写中间脚本)?-r
--help
-r
因为我不认为上面很清楚,所以我无法使用该选项。以下是我正在运行的两个版本的PHP的输出。-r
php -h
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>.
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 中的错误,字符 2:找不到选项 r
很抱歉造成混淆。