There are several ways to print a new line in PHP. A line break is quite an important factor in content formatting and organization. Also, content management is required for a better user experience of the website.
Index
Line break helps to make content more readable with proper usage. There are many ways to enter a new line in PHP. Here we will discuss all the available methods to print a new in PHP.
Whatever we echo or print in PHP gives us output in a browser where “\n” and “\r\n” do not work. The browser requires HTML tags to understand content formatting.
In HTML <br> is the official tag of a line break. We can use the <br> tag where we want to print a new line in HTML. Even more, we can directly print it in PHP.
Example
<?php
echo "HTML <br>
CSS <br>
JavaScript";
?>
Output
HTML
CSS
JavaScript
It is easy to just put a <br> tag in PHP string and echo it. But what if there are lots of data available to print?
At that time, writing <br> code in lengthy passages would be a waste of our valuable time. Here is an easy way to do that in PHP.
PHP has a default function named nl2br(), which converts all the line breaks to <br> tag in any PHP string.
Example
<?php
// Line break notation using \n
$str = "HTML\nCSS\nJavaScript";
echo nl2br($str); echo "<br>-----------------------<br>";
// Line break notation using PHP_EOL
$str2 = "HTML".PHP_EOL."CSS".PHP_EOL."JavaScript";
echo nl2br($str2);
echo "<br>-----------------------<br>";
// Line break notation using string break using Enter Key
$str3 = "HTML
CSS
JavaScript";
echo nl2br($str3);
?>
Output
HTML
CSS
Javascript
-----------------------
HTML
CSS
Javascript
-----------------------
HTML
CSS
Javascript
Read Also: Find Array Length in PHP
Printing line break helps to beautify our website formatting and provides high readability for users. I hope now you have a complete understanding of the PHP line break and ways to do it.
Enjoy Programming 🙂
There is a reason big-name companies like CNN use WordPress. WordPress is a popular content…
In this tutorial, I'm going to show you how to install MySQL on your computer.…
Download Turbo C++ for windows 10 in just 7 Mb and run your first C++…
We can redirect any webpage to any other or redirect the whole domain or website…
There are lots of methods to redirect pages, like refresh-redirect from META tag, redirect from…
Include files in PHP are used in appending various global or config files. We can…