如何在 PHP 中建立新行
Minahil Noor
2023年1月30日
PHP
PHP Array

本文將介紹在 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.
Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe