7

I want to create a command line program using PHP. How do I design the program's I/O? I can send output as text. I am wondering about the specific output syntax. For example: In HTML i use <br/> to pass to a new line. How can I do this using terminal/file output? Is there a reference for terminal/file oriented programming in PHP?

4
  • 3
    why the downvotes and vote to close? it seems like a perfectly reasonable question to me. At least leave a constructive comment if you're going to vote to to close. Commented Apr 30, 2009 at 13:13
  • might have to do with the original question: stackoverflow.com/revisions/806521/… . still wondering how Yuval F managed to shape it so well. Commented May 1, 2009 at 13:06
  • might have to do with the original question: stackoverflow.com/revisions/806521/… . still wondering how Yuval F managed to shape it so well. Commented May 1, 2009 at 13:06
  • 1
    But still voting down and closing question instead of editing it is unreasonable Commented May 1, 2009 at 21:02

2 Answers 2

11

Writing command-line PHP scripts is quite simple, actually. You output text in the exact same way you would normally: print and echo both print text to the console. The only difference here is that you can't use HTML tags for formatting, since your code isn't being interpreted by a web browser (i.e. "\n" will actually create a visible line break, not <br />).

Reading input from stdin is a little trickier, but all it really involves is essentially using some of the file reading functions (e.g. fgets(), fgetc(), fscanf()) and passing in STDIN as the file path (or php://stdin, depending on how new your version of PHP is).

And yes, there is a reference for command-line programming in PHP on php.net. It covers pretty much everything you need to know to work with PHP in a command-line environment.

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

Comments

4

When writing a CLI script consider ending lines with PHP_EOL so it will be cross platform compatible with UNIX ( \n ), Windows ( \n\r ) and Mac ( \r ). And when you whant to print it as html use the PHP's nl2br function.

Comments

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.