Can I receive input from both a pipe, and a heredoc, and use them both from within php on the command line.
I want to do something like this:
bash$ ls -l | php <<'code'
<?php
echo $piped;
?>
code
Which should return the result of ls -l
Also, can I use php -R
with heredoc input for php script?
Piping
ls -l | php -r 'print_r(file("php://stdin"));'
Heredoc
$ php <<CODE
<?php
echo "Hello World\n";
?>
CODE
Hello World
Combined
$ ls -l | php <<'CODE'
<?php
$f = file("php://stdin");
foreach($f as $k=>$v){
echo "[$k]=>$v";
}
?>
Program Finished
CODE
[0]=><?php
[1]=>$f = file("php://stdin");
[2]=>foreach($f as $k=>$v){
[3]=>echo "[$k]=>$v";
[4]=>}
[5]=>?>
[6]=>Program Finished
Program Finished
Note: When you use Here Documents for php
command the newly added php
codes overrides the previous stdin
Regarding the -R
part of the question:
-R
/--process-code
PHP code to execute for every input line. Added in PHP 5.
There are two special variables available in this mode:
$argn
and$argi
.$argn
will contain the line PHP is processing at that moment, while$argi
will contain the line number. Docs
If I understood your question right, you're looking for the $argn
variable. Heredoc should be support by your bash.
Edit: Err, just invoke with the value over multiple lines:
$ ls -l | php -R '
printf("#%02d: %s\n", $argi, $argn);
'
(I think it's easier to use the single quote for the switch)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With