如何在php中使用STDOUT

时间:2022-06-11 00:04:07

I am running into a project that read a stream from a txt file, so in the CLI, i write:

我正在运行一个从txt文件读取流的项目,所以在CLI中,我写道:

cat texte.txt|php index.php

In my index.php file i read the stream:

在我的索引。php文件我读流:

$handle = fopen ("php://stdin","r");

Now i have a $result that contains the result of my processing file and i want to output it with STDOUT, I looked in the manual, but I don't know how to use it, can you please make me a use example.

现在我有一个$result,它包含了我的处理文件的结果,我想用STDOUT输出它,我在手册中找过了,但是我不知道如何使用它,你能给我一个用例吗?

3 个解决方案

#1


37  

Okay, let me give you another example for the STDIN and STDOUT usage.

好的,让我再给你一个STDIN和STDOUT用法的例子。

In PHP you use these two idioms:

在PHP中,您使用以下两个习惯用法:

 $input = fgets(STDIN);

 fwrite(STDOUT, $output);

When from the commandline you utilize them as such:

当从命令行中,你可以使用它们:

 cat "input.txt"  |  php script.php   >  "output.txt"

 php script.php  < input.txt  > output.txt

 echo "input..."  |  php script.php   |  sort  |  tee  output.txt

That's all these things do. Piping in, or piping out. And the incoming parts will appear in STDIN, whereas your output should go to STDOUT. Never cross the streams, folks!

这就是所有这些东西的作用。管进,管出。输入的部件将显示在STDIN中,而输出应该显示在STDOUT中。别过河,伙计们!

#2


15  

The constants STDIN and STDOUT are already resources, so all you need to do is

STDIN和STDOUT的常量已经是资源,所以您需要做的就是。

fwrite(STDOUT, 'foo');

See http://php.net/manual/en/wrappers.php

参见http://php.net/manual/en/wrappers.php

php://stdin, php://stdout and php://stderr allow direct access to the corresponding input or output stream of the PHP process. The stream references a duplicate file descriptor, so if you open php://stdin and later close it, you close only your copy of the descriptor-the actual stream referenced by STDIN is unaffected. Note that PHP exhibited buggy behavior in this regard until PHP 5.2.1. It is recommended that you simply use the constants STDIN, STDOUT and STDERR instead of manually opening streams using these wrappers.

php://stdin、php://stdout和php://stderr允许直接访问php进程的相应输入或输出流。流引用一个重复的文件描述符,所以如果打开php://stdin,然后关闭它,那么只关闭描述符的副本——stdin引用的实际流不受影响。注意,PHP在这方面表现出错误行为,直到PHP 5.2.1。建议您使用常量STDIN、STDOUT和STDERR,而不是使用这些包装器手动打开流。

#3


12  

this should work for you

这应该对你有用。

$out = fopen('php://output', 'w'); //output handler
fputs($out, "your output string.\n"); //writing output operation
fclose($out); //closing handler

#1


37  

Okay, let me give you another example for the STDIN and STDOUT usage.

好的,让我再给你一个STDIN和STDOUT用法的例子。

In PHP you use these two idioms:

在PHP中,您使用以下两个习惯用法:

 $input = fgets(STDIN);

 fwrite(STDOUT, $output);

When from the commandline you utilize them as such:

当从命令行中,你可以使用它们:

 cat "input.txt"  |  php script.php   >  "output.txt"

 php script.php  < input.txt  > output.txt

 echo "input..."  |  php script.php   |  sort  |  tee  output.txt

That's all these things do. Piping in, or piping out. And the incoming parts will appear in STDIN, whereas your output should go to STDOUT. Never cross the streams, folks!

这就是所有这些东西的作用。管进,管出。输入的部件将显示在STDIN中,而输出应该显示在STDOUT中。别过河,伙计们!

#2


15  

The constants STDIN and STDOUT are already resources, so all you need to do is

STDIN和STDOUT的常量已经是资源,所以您需要做的就是。

fwrite(STDOUT, 'foo');

See http://php.net/manual/en/wrappers.php

参见http://php.net/manual/en/wrappers.php

php://stdin, php://stdout and php://stderr allow direct access to the corresponding input or output stream of the PHP process. The stream references a duplicate file descriptor, so if you open php://stdin and later close it, you close only your copy of the descriptor-the actual stream referenced by STDIN is unaffected. Note that PHP exhibited buggy behavior in this regard until PHP 5.2.1. It is recommended that you simply use the constants STDIN, STDOUT and STDERR instead of manually opening streams using these wrappers.

php://stdin、php://stdout和php://stderr允许直接访问php进程的相应输入或输出流。流引用一个重复的文件描述符,所以如果打开php://stdin,然后关闭它,那么只关闭描述符的副本——stdin引用的实际流不受影响。注意,PHP在这方面表现出错误行为,直到PHP 5.2.1。建议您使用常量STDIN、STDOUT和STDERR,而不是使用这些包装器手动打开流。

#3


12  

this should work for you

这应该对你有用。

$out = fopen('php://output', 'w'); //output handler
fputs($out, "your output string.\n"); //writing output operation
fclose($out); //closing handler