PHP で改行を作成する
Minahil Noor
2023年1月30日
PHP
PHP Array

この記事では、\n
や \r\n
などのエスケープシーケンスを利用して PHP で改行する方法を紹介します。
PHP で改行するための \n
エスケープシーケンスの使用法
PHP では、改行を行うためにさまざまな方法がありますが、その中でも最もシンプルな方法は \n
エスケープシーケンスを使用することです。このエスケープシーケンスをプログラムに含める際には、二重引用符を使用する必要があることに注意してください。コマンドを実行するための正しい構文は以下の通りです。
echo("\n");
以下のプログラムは、\n
エスケープシーケンスを使用して PHP で改行を行う方法を示しています。
<?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.
チュートリアルを楽しんでいますか? <a href="https://www.youtube.com/@delftstack/?sub_confirmation=1" style="color: #a94442; font-weight: bold; text-decoration: underline;">DelftStackをチャンネル登録</a> して、高品質な動画ガイドをさらに制作するためのサポートをお願いします。 Subscribe