Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Piping PHP file content to PHP command in CLI

My question relate to the use of PHP in CLI. I don't know why the piping of the content of a PHP file to the PHP command works:

    cat file.php | php  

like in the installation of Composer Composer Installation:

    curl -sS https://getcomposer.org/installer | php
like image 597
Hakim Avatar asked Mar 23 '26 08:03

Hakim


1 Answers

If you don't give any argument to PHP, it reads from standard input (commonly called stdin). If your output buffering is disabled, you can try to run php without argument, and type <?php echo "test\n"; + Enter, you'll see "test". stdin is basically the stream where your keyboard writes, and stdout is basically your terminal, where echo writes.

enter image description here

But the pipe ( | ) changes that behaviour : the standard output of the first program becomes the standard input of the second one.

enter image description here

This is a quite powerful thing our nix system shells offer :-).

like image 93
Alain Tiemblo Avatar answered Mar 25 '26 21:03

Alain Tiemblo