How to Add a Line Break Within Echo in PHP

If you’re a PHP developer, you probably know how to display text on a web page using the echo function.

However, sometimes you need to add line breaks in your text to make it more readable.

In this tutorial, we’ll show you how to add line breaks within the echo function in PHP.


Introduction to Line Breaks in PHP

Line breaks are used to separate one line of text from another.

They are particularly useful when you want to display text in multiple lines on a web page.

In PHP, you can add a line break using the special escape sequence “\n”.

Adding Line Breaks Within Echo in PHP

To add a line break within the echo function in PHP, you need to include the escape sequence “\n” in your text.

Here’s an example:

echo "Line 1\nLine 2\nLine 3";

In this example, we’ve used the escape sequence “\n” to separate the text into three different lines.

When you run this code, it will display the text in three separate lines on your web page.

Using HTML to Add Line Breaks in PHP

You can also add line breaks in PHP using HTML. The HTML line break tag is <br>. Here’s an example:

echo "Line 1<br>Line 2<br>Line 3";

In this example, we’ve used the HTML line break tag to separate the text into three different lines.

When you run this code, it will display the text in three separate lines on your web page.


Conclusion

Adding line breaks within the echo function in PHP is easy and can help to make your text more readable.

Whether you choose to use the escape sequence “\n” or the HTML line break tag, the result will be the same.

Just remember to use the appropriate method based on your specific requirements.