Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use STDOUT in php

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

cat texte.txt|php index.php 

In my index.php file i read the stream:

$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.

like image 768
Malloc Avatar asked Jan 08 '12 22:01

Malloc


People also ask

How do I use stdout in 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.

What is stdin PHP?

php://stdin, php://stdout and php://stderr allow direct access to standard input stream device, standard output stream and error stream to a PHP process respectively. Predefined constants STDIN, STDOUT and STDERR respectively represent these streams.

Can you Fopen stdout?

Stream stdout is opened with fopen("stdout", "w") . Stream stderr is opened with fopen("stderr", "w") .

What does PHP output do?

php://output ¶ php://output is a write-only stream that allows you to write to the output buffer mechanism in the same way as print and echo.


1 Answers

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

In PHP you use these two idioms:

 $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!

like image 163
mario Avatar answered Oct 06 '22 03:10

mario