PHP echo Multiple Lines – When we think of printing something in PHP, we think of echo or print, and that’s what we use most of the time if not all of the time. However, PHP has so many built-in features that we could be concerned about its scalability.
There will be occasions in our projects when we need to input a lot of text and long words, and utilizing echo or print at those times will be a bit messy and time-consuming.
So In this article, we will see multiple methods of how to write PHP echo multiple lines with coding examples.
The following methods can be used to create PHP echo Multiple Lines.
- Using escape sequences
- Using concatenation assignment operator
- Heredoc and Nowdoc Syntax
Using escape sequences
The \n escape sequences can be used to declare multiple lines in a string. Let’s have a look at a simple code example.
Example:
<?php
//declaring multiple lines using the new line escape sequence
$var="We\nWelcome\nYou\nOn\nOur\nSofthunt\nWebsite";
echo $var;
?>
Output:

Using concatenation assignment operator
To concatenate two strings, we may use the concatenation assignment operator .= and the PHP_EOL to mark the end of the line. Let’s have a look at a simple code example.
Example:
<?php
$var1="We". PHP_EOL;
$var2="Welcome". PHP_EOL;
$var3="You". PHP_EOL;
$var4="On". PHP_EOL;
$var5="Our". PHP_EOL;
$var6="Softhunt". PHP_EOL;
$var7="Website";
$var1.=$var2.=$var3.=$var4.=$var5.=$var6.=$var7;//concatenating the string into $var1
echo $var1 //printing concatenated string
?>
Output:

Heredoc and Nowdoc Syntax For PHP echo Multiple Lines
To write multiple-line string variables directly, we can use the PHP Heredoc or PHP Nowdoc syntax. Heredoc differs from nowdoc in that it makes use of double-quoted strings. For escape sequences, etc., parsing is performed inside a heredoc, but a nowdoc employs single-quoted texts, and hence parsing is not performed.
Note: In the heredoc and nowdoc syntaxes, the delimiter must always be at the beginning of a line, without any spaces, characters, etc.
Now let’s understand their concepts via some coding examples:
Example 01: Using Heredoc variable
<?php
//Heredoc variable
$var=<<<EOD
We
\tWelcome
You
On
Our
Softhunt
Website
EOD;
echo $var;
?>
Output:

Example 02: Using Nowdoc variable
<?php
//Nowdoc variable
$var=<<<'EOD'
We
\tWelcome
You
On
Our
Softhunt
Website
EOD;
echo $var;
?>
Output:

That’s all for this article if you have any confusion contact us through our website or email us at [email protected] or by using LinkedIn
Suggested Articles:
Hello to every single one, it’s truly good for me to
pay a quick visit to this site, it consists of precious Information.
Thank you