我希望能够在命令行上运行一行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

抱歉搞混了.

推荐答案

是的,PHP 5.2's cli SAPI分钟后就到了.

如果您不能升级,并且在PHP5.2中没有这样的选项(我手头没有可以测试的选项),您可以这样做:

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

hi

原件:

确实有-r个选项(尽管我不确定PHP5.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

Php相关问答推荐

用户信息更新期间Laravel邮箱验证问题

make dd()如何正确工作?¨

如何使用`preg_replace`删除重复的空格字符

无法在WooCommerce中发送客户笔记邮箱通知

如何确保所有语言文件都使用Laravel中的新密钥进行更新?

在PHP 8.2 Gloogle云引擎上加载大型php文件速度缓慢

PHP -流加密启用服务器切断字节

将数据推入所有子数组条目

强制客户在注册时 Select WooCommerce角色

按类别层次 struct 过滤 Laravel 集合

如何在 PHP 中以 hh:mm:ss 格式获取两个 strtotime datetime/s 之间的时间差

WooCommerce:如果购物车中已存在产品,则增加购物车商品数量

WooCommerce:如果订单总数为零,则隐藏本地取货运输选项

WooCommerce单个产品页面上启用额外选项卡的产品元框字段

Laravel使用另一张表进行关联查询

PHP 中是否有 is_closure() 函数?

计算组合指数

基于其他数组创建多维数组 struct

图像不会显示在生产中的 Laravel 应用程序中

Laravel - 关联与设置观察者事件的 ID