What is PHP syntax for formatting file / terminal output?

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

+7
command-line design file php
source share
2 answers

Writing PHP scripts on the command line is actually quite simple. You output the text in the same way as usual: print and echo both print the text to the console. The only difference here is that you cannot use HTML tags for formatting, as your code is not interpreted by the web browser (ie, "\n" will actually create a visible line break, not <br /> ).

Reading input from stdin is a bit more complicated, but all it really does is essentially use some file reader functions (e.g. fgets() , fgetc() , fscanf() ) and pass the file path (or php://stdin to STDIN php://stdin , depending on how the new version of PHP is).

And yes, there is a link for command line programming in PHP at php.net. It covers almost everything you need to know in order to work with PHP in a command line environment.

+11
source share

When writing a CLI script, consider the end lines of PHP_EOL , so it will be compatible with the UNIX ( \n ), Windows ( \n\r ) and Mac ( \r ) platforms. And when you want to print it as html, use the PHP function nl2br .

+4
source share

All Articles