文件/终端输出格式的PHP语法是什么?

时间:2022-10-26 22:27:39

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?

我想用PHP创建一个命令行程序。如何设计程序的I / O?我可以将输出作为文本发送。我想知道具体的输出语法。例如:在HTML中,我使用
转到新行。如何使用终端/文件输出执行此操作? PHP中有面向终端/文件的编程参考吗?

2 个解决方案

#1


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 />).

实际上,编写命令行PHP脚本非常简单。您可以按照与通常相同的方式输出文本:将打印文本打印并回显到控制台。这里唯一的区别是你不能使用HTML标签进行格式化,因为你的代码没有被网络浏览器解释(即“\ n”实际上会创建一个可见的换行符,而不是
)。

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).

从stdin读取输入有点棘手,但它真正涉及的主要是使用一些文件读取函数(例如fgets(),fgetc(),fscanf())并传入STDIN作为文件路径(或php:/ / stdin,取决于你的PHP版本是多么新)。

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.

是的,在php.net上有一个PHP命令行编程的参考。它涵盖了在命令行环境中使用PHP时需要了解的所有内容。

#2


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.

在编写CLI脚本时,请考虑使用PHP_EOL结束行,以便它与UNIX(\ n),Windows(\ n \ r)和Mac(\ r \ n)跨平台兼容。当你想用html打印它时,使用PHP的nl2br函数。

#1


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 />).

实际上,编写命令行PHP脚本非常简单。您可以按照与通常相同的方式输出文本:将打印文本打印并回显到控制台。这里唯一的区别是你不能使用HTML标签进行格式化,因为你的代码没有被网络浏览器解释(即“\ n”实际上会创建一个可见的换行符,而不是
)。

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).

从stdin读取输入有点棘手,但它真正涉及的主要是使用一些文件读取函数(例如fgets(),fgetc(),fscanf())并传入STDIN作为文件路径(或php:/ / stdin,取决于你的PHP版本是多么新)。

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.

是的,在php.net上有一个PHP命令行编程的参考。它涵盖了在命令行环境中使用PHP时需要了解的所有内容。

#2


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.

在编写CLI脚本时,请考虑使用PHP_EOL结束行,以便它与UNIX(\ n),Windows(\ n \ r)和Mac(\ r \ n)跨平台兼容。当你想用html打印它时,使用PHP的nl2br函数。