command-line - 在命令行执行一串PHP代码

标签 command-line php

我希望能够在命令行上运行一行 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 选项对我来说是 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>.

没有 -r 选项。当我尝试使用 -r 选项时,我得到:

Error in argument 1, char 2: option not found r

很抱歉给您带来了困惑。

最佳答案

是的,它在 PHP 5.2's cli SAPI 中.

如果您无法升级并且 PHP 5.2 中没有这样的选项(我手头没有它可以测试),您可以这样做:

echo "<?php echo \"hi\\n\";" | php

hi

原文:

确实有一个 -r 选项(虽然我不确定 PHP 5.2):

php -r "echo 'hi';";

hi

只要确保您使用的是 PHP 的命令行版本。 php --version 应该给你这样的东西(注意“cli”):

php --version

PHP 5.3.0 (cli) (built: May 20 2010 19:05:12) (DEBUG)
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2009 Zend Technologies

关于command-line - 在命令行执行一串PHP代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2954540/

相关文章:

sql - 如何在不使用 GUI 的情况下创建 SQL Server 代理?

android - 从命令行安装 android 旧系统镜像 (ABI)

php - 如何在没有 ffmpeg 的情况下在 php 中创建缩略图并将视频转换为 mp4?

java - 为什么 JOptionPane 没有出现?

c++ - 如何将值从 C++ 可执行文件传递到 Rails 应用程序?

windows - 从命令行关闭窗口(在 Windows 上)

php - 在 mysql_real_escape_string 之后,在 Twitter 中出现的撇号前面有一个反斜杠

javascript - 使用 javascript 根据数组的相同元素对数组进行分组

javascript - PHP header ("Location:index.html") 或 php 中的 javascript 无法正常工作

php - 我如何修改此 MySQL 查询来查找排名?