如何在 PHP 中创建新行

Minahil Noor 2023年1月30日
  1. 在 PHP 中使用\n 转义序列来创建新行
  2. 在 PHP 中使用\r\n 转义序列创建新行
如何在 PHP 中创建新行

本文将介绍在 PHP 中利用转义序列创建新行的不同方法,如\n\r\n

在 PHP 中使用\n 转义序列来创建新行

在 PHP 中,我们有不同的方法来创建新行,其中最简单的方法是使用\n 转义序列。请注意,当你在程序中包含这个转义序列时,需要使用双引号。执行命令的正确语法如下。

echo("\n");

下面的程序显示了我们如何在 PHP 中使用\n 转义序列来创建一个新行。

<?php
echo("There is a new line after this. \n");
echo("This program shows a new line. \n");
?>

输出:

There is a new line after this. 
This program shows a new line. 

我们可以使用两次\n 函数在两行文本之间创建一个行距。

<?php
echo("There is a new line after this. \n\n");
echo("This program shows a new line. \n");
?>

输出:

There is a new line after this.
    
This program shows a new line. 

在 PHP 中使用\r\n 转义序列创建新行

在 PHP 中,我们也可以使用\r\n 转义序列来创建新的行;此外,它还返回回车,重新设置指针并从左边开始。正确使用它的语法如下。

echo("\r\n");

下面是应用此方法的程序:

<?php
echo("There is a new line after this. \r\n");
echo("This program shows a new line. \n");
?>

输出:

There is a new line after this. 
This program shows a new line.

相关文章 - PHP Array