代码之家  ›  专栏  ›  技术社区  ›  Matthew J Morrison

在命令行上执行一串PHP代码

  •  50
  • Matthew J Morrison  · 技术社区  · 15 年前

    :~> perl -e "print 'hi';"
    :~> python -c "print 'hi'"
    :~> ruby -e "puts 'hi'"
    

    我希望能够做到:

    :~> php "echo 'hi';"
    

    我读到有一个-r选项可以满足我对php的需求,但是当我尝试使用它时,它似乎不可用。我试过使用PHP5.2.13和PHP4.4.9,但都没有可用的-r选项。

    我写了这个脚本(我称之为runïphp.php),它很管用,但我不是它的超级粉丝,因为我觉得应该有一个更“正确”的方法来实现它。

    #!/usr/bin/php5 -q
    <?php echo eval($argv[1]); ?> 
    

    我的问题是:有a-r选项吗?如果是这样,为什么在我运行--help时它不可用?如果没有-r选项,那么最好的方法是什么(如果可能的话,不用编写中间脚本)?

    谢谢!

    ===编辑===

    因为我觉得上面不是很清楚,-r选项对我来说不可用。下面是我正在运行的两个php版本的php-h输出。

    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>.
    

    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
    

    抱歉给你添麻烦了。

    5 回复  |  直到 14 年前
        1
  •  106
  •   Artefacto    15 年前

    是的,就在那儿 PHP 5.2's cli SAPI

    编辑:如果您无法升级,并且在PHP5.2中没有这样的选项(我手头没有这个选项可供测试),您可以这样做:

    glopes@nebm:~$ echo "<?php echo \"hi\\n\";" | php
    hi
    

    确实有一个 -r

    D:\>php -r "echo 'hi';";
    hi
    

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

    D:\>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
    
        2
  •  16
  •   Damian W.    11 年前

    在新版本的PHP中,您只需键入“PHP-a”,然后跳入一个交互式模式,在那里您可以尝试使用PHP。

        3
  •  5
  •   Sumoanand    11 年前

    最后不需要额外的分号。

    php -r "echo 'hi';" 而不是 php -r "echo 'hi';";

    另一个示例(在命令行获取当前时间戳):

    php -r 'print time()."\n";'
    
        4
  •  4
  •   Benubird    9 年前

    php -r "passthru(file_get_contents('php://stdin'));"
    

    它可以让你从标准的管道,像这样:

    echo -n "echo 'test';" | php -r "passthru(file_get_contents('php://stdin'));"
    

    但是,要回答原来的问题,如果没有-r标志,也可以使用-f标志-just pass stdin作为要打开的文件: php -f /dev/stdin

    <?php

    echo -ne " <?php\necho 'test';" | php -f /dev/stdin
    
        5
  •  3
  •   allnightgrocery    15 年前

    请看上的这一页 PHP command line features ,如果你还没有。有一些关于基于操作系统和双引号或单引号的问题的帖子。

    我还会检查PHP信息

    看看PHP是否 compiled with CLI support disabled (—禁用cli)。