$ _SERVER未使用命令行-r选项设置?
When I try to print the $_SERVER variable from a command line php, it thinks $_SERVER is not set.
$ php -r "print_r($_SERVER);"
Warning: print_r() expects at least 1 parameter, 0 given in Command line code on line 1
However, when it's in a file, running it from the command line has it set
$ cat test.php
<?
print_r($_SERVER);
$ php test.php
Array
(
[TERM] => xterm
[SHELL] => /bin/bash
[SSH_CLIENT] => 192.168.1.101 49319 22
[SSH_TTY] => /dev/pts/0
...
Why?
当我尝试从命令行php打印$ _SERVER变量时,它认为$ _SERVER未设置。
$ php -r“print_r($ _ SERVER);”
警告:print_r()需要至少1个参数,0在第1行的命令行代码中给出
代码> pre>
但是,当它在文件中时,从命令行运行它已设置 p>
$ cat test.php \ n&lt;?
print_r($ _ SERVER);
$ php test.php
Array
(
[TERM] =&gt; xterm
[SHELL] =&gt; / bin / bash
[SSH_CLIENT] =&gt; 192.168.1.101 49319 22
[SSH_TTY] =&gt; / dev / pts / 0
...
code> pre>
为什么? p>
div>
You need to escape the $ character on the command line.
php -r "print_r(\$_SERVER);"
Otherwise the shell will think it's a shell variable called _SERVER (which you don't have set to anything) and so what's actually been run is php -r "print_r();"
, which is why you get the error "print_r() expects at least 1 parameter, 0 given".
From the official documentation
You may or may not find any of the following elements in $_SERVER. Note that few, if any, of these will be available (or indeed have any meaning) if running PHP on the command line.