12

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?

5
  • @DaveRandom In bash, Here String needs three < and Here Document two <. Commented Dec 27, 2011 at 21:29
  • @Shiplu I retract my pedantry and admit defeat (in that I had never come across that in the context of bash before). Comment shall be deleted... Commented Dec 27, 2011 at 21:32
  • @DaveRandom: Pedanticism requires that you stop writing apostrophes in inappropriate places. Commented Dec 27, 2011 at 21:37
  • @TomalakGeret'kal I'm not even going to begin to get into a debate about the precise semantic use of the possessive apostrophe, it is a debate that will never end. You may enjoy this... Commented Dec 27, 2011 at 21:52
  • @DaveRandom: You should follow that advice... I'm not aware of any debate over when an apostrophe should be used; only of many people who get it wrong. Also please stop abusing commas. Commented Dec 27, 2011 at 21:59

2 Answers 2

18

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

Sign up to request clarification or add additional context in comments.

2 Comments

So what you are saying is that when you combine piping and heredoc, the piped part is trampled by the heredoc part, and can not be retrieved?
@BillyMoon yes. It can not be retrived.
3

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)

6 Comments

I can not figure out how to pass a heredoc to php -R instead of a string, and I am not sure that it is possible directly.
My bad, just try a multiline "string" instead of a heredoc. Edited the question to reflect that.
Wow, multi-line string - what a revelation. Simple, and elegant - I ad no idea... I have been spending too much time writing javascript lately!
I found that if I use ' to define the multi-line string, I don't have to escape so much of my php code, but is there any way I can avoid escaping the ' inside my php code - so probably use a different token to delimit a multi-line string?
@BillyMoon: Better use the single quote for the parameter with PHP code. You don'T need to escape that much.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.