zsh:错误的数学表达式:操作数预期在`' HTTP_HOST ...'

时间:2013-12-17 17:40:19

标签: php zsh

我正在尝试从终端运行以下php:

php -r "error_reporting(E_NONE); $_SERVER['HTTP_HOST'] = 'localhost';
require('./config/site.php'); 
echo json_encode(array('HOST' => DB_SERVER, 'USER' => DB_USERNAME, 'PASS' => DB_PASSWORD, 'NAME' => DB_DATABASE));"

我收到以下错误:

zsh: bad math expression: operand expected at `'HTTP_HOST...'

转义=无效并转义[会产生PHP解析错误:

PHP Parse error: syntax error, unexpected '[', expecting identifier (T_STRING) in Command line code on line 1

我在这里做错了什么?如何从终端运行上面的代码?

1 个答案:

答案 0 :(得分:4)

因为你在双引号中运行PHP代码,PHP解释器试图用$ _SERVER ['HTTP_HOST']的值替换$ _SERVER ['HTTP_HOST'],以使其工作,你需要逃脱$

php -r "error_reporting(E_NONE); \$_SERVER['HTTP_HOST'] = 'localhost';
require('./config/site.php'); 
echo json_encode(array('HOST' => DB_SERVER, 'USER' => DB_USERNAME, 'PASS' => DB_PASSWORD, 'NAME' => DB_DATABASE));"

我在其中一个盒子上进行了测试以验证。

不逃避

[root@exia ~]# php -r "var_dump($_SERVER['HTTP_HOST']);"
array(1) {
  [0] =>
  string(9) "HTTP_HOST"
}

逃脱

[root@exia ~]# php -r "var_dump(\$_SERVER['HTTP_HOST']);"
NULL

另一个好的措施

[root@exia ~]# php -r "error_reporting(E_NONE); \$_SERVER['HTTP_HOST'] = 'localhost';
var_dump(\$_SERVER['HTTP_HOST']);"
string(9) "localhost"