PHP将大量文本写入STDERR

Ed *_*eal 12 php stdout echo stderr

STDOUT在PHP中编写大块时,可以这样做:

echo <<<END_OF_STUFF
lots and lots of text
over multiple lines
etc.etc
END_OF_STUFF;
Run Code Online (Sandbox Code Playgroud)

(即heredoc)

我需要做类似的事情,但是STDERR.是否有另一个命令,echo但使用STDERR相反?

Mch*_*chl 20

是的,使用php:// stream wrapper:http://php.net/manual/en/wrappers.php.php

$stuff = <<<END_OF_STUFF
lots and lots of text
over multiple lines
etc.etc
END_OF_STUFF;

$fh = fopen('php://stderr','a'); //both (a)ppending, and (w)riting will work
fwrite($fh,$stuff);
fclose($fh);
Run Code Online (Sandbox Code Playgroud)

  • 而不是`fopen`东西,我只使用`fwrite(STDERR,$ stuff);` - 希望不必一直创建变量. (15认同)
  • 您无需打开错误流.[它已经打开并分配给常量`STDERR`.](http://php.net/manual/en/features.commandline.io-streams.php) (10认同)
  • 这是正确的,但假设我们在CLI中,这没有明确给出问题:) (7认同)

Lix*_*Lix 18

对于简单的解决方案 - 试试这个

file_put_contents('php://stderr', 'This text goes to STDERR',FILE_APPEND);
Run Code Online (Sandbox Code Playgroud)

FILE_APPEND参数将追加数据而不会覆盖它.您还可以使用fopenfwrite函数直接写入错误流.

更多信息可以在http://php.net/manual/en/features.commandline.io-streams.php找到