How to make multi-line Strings in PHP
  John Mwaniki /   16 Dec 2021

How to make multi-line Strings in PHP

A string is a series of characters (may include alphabets, numeric figures, or special symbols and characters).

In most cases and in most programming languages, a string is specified by enclosing a group of text within single or double-quotes.

Doing this by default makes single-line strings that take only one line when printed on a screen. The string extends to the next only if it reaches the end of the line.

Example

<?php
$str = "PHP is a good programming language. You cannot convince me otherwise.";
echo $str;

Output

PHP is a good programming language. You cannot convince me otherwise.

For some reason, you may want to create a string that spans across multiple lines without necessarily having to reach the end of the line. These strings are referred to as multi-line strings.

A multi-line string can be defined as a string whose value exceeds one line.

In this article, we will explore the different ways in which you can accomplish creating multi-line strings in PHP with aid of examples.

Below are ways of making multiline strings in PHP:

1. Use of Heredoc Syntax

This is an in-built way of creating multi-line strings in PHP. Heredoc text behaves just like a double-quoted string, but without the double-quotes. To do it:

  • Write the operator "<<<" followed by a delimiter identifier. This delimiter can be any text of your choice. The delimiter should be immediately followed by a newline.
  • Write the string value (can be a single line of text or text spanning across multiple lines).
  • Write the same delimiter identifier is again in a new line to mark the end of the string. This closing identifier must be alone in a newline followed by a semi-colon (;) and with no whitespace before or after it.
<?php
$str = <<<ANYTHING
This is line 1
This is line 2
And guess what... this is line 3
ANYTHING; 
echo $str;

Output

This is line 1
This is line 2
And guess what... this is line 3

2. Use of Nowdoc Syntax

This method is very similar to heredoc, only that its delimiter is enclosed in single quotes. The Nowdoc text behaves like a single-quoted string, but without the quotes, ie. it cannot parse variables and special characters.

<?php
$str = <<<'ANYTEXT'
This is line 1 text
And this here is line 2 text
'ANYTEXT'; 

Output

This is line 1 text
And this here is line 2 text

3. Strings concatenation with PHP_EOL constant

Concatenation is the operation of joining two or more strings to form one string. In PHP, we use the dot operator (.) to concatenate strings.

Examples

<?php
//Example 1
$str1 = 'PHP is a great language.';
$str2 = ' You cannot convince me otherwise.';
$str3 = $str1.$str2;
echo $str3; 
// Output: PHP is a great language. You cannot convince me otherwise.

//Example 2
$str = 'PHP is a great language.';
$str .= ' You cannot convince me otherwise.';
echo $str; 
// Output: PHP is a great language. You cannot convince me otherwise.

PHP_EOL is a predefined constant in PHP which in full means "End Of Line".

It's used with strings to make them multi-line. It adds a line break at the point where you concatenate it to a string.

It can be used with both single and double-quoted strings but is the only way to make a multiline string from a single-quoted string.

To make a string value span across multiple lines, just concatenate PHP_EOL at the desired end of the line.

Example 1

<?php
$str1 = 'My name is John.';
$str2 = 'I am a developer.';
$str3 = 'I like coding in PHP.';
$str4 = $str1.PHP_EOL.$str2.PHP_EOL.$str3;
echo $str4; 

Output

My name is John.
I am a developer.
I like coding in PHP.

Example 2

<?php
$str = 'My name is John.'.PHP_EOL;
$str .= 'I am a developer.'.PHP_EOL;
$str .= 'I like coding in PHP.';
echo $str;

Output

My name is John.
I am a developer.
I like coding in PHP.

4. Use of escape sequences

This method is applicable for double quotes ("). You can also use string escape sequences with heredoc syntax as these two methods are able to parse special characters.

These escape sequences include \n and \r\n.

The \n is a newline character used to add a line break at the point it is put and makes the other text after it to start in a new line. If you want to add more space between the lines, you can use more than one \n in your string.

Example

<?php
$str = "My name is John.\nI am a developer.\nI like coding in PHP. \n\nPHP is awesome!";
echo $str; 

Output

My name is John.
I am a developer.
I like coding in PHP.

PHP is awesome!

We can also use the \r\n escape sequence to write multi-line strings. It creates the new line as well because it is the carriage return. The carriage return resets the pointer and starts it from the left.

Example

<?php
$str = "My name is John.\r\nI am a developer.\r\nI like coding in PHP.";
echo $str; 

Output

My name is John.
I am a developer.
I like coding in PHP.

While all the above examples work and echos multiple-line strings just as they appear in the code, that may not be the case you see while running the code on a web browser. Your web browser displays them as a single line of text.

PHP is a server-side language. Its code is executed on a web server, and the result is returned to the browser as plain HTML. Therefore, what you see on a web browser is the output that has already been converted to HTML.

HTML does not recognize the PHP newline characters (\n and \r\n). It also does not really care about white spaces so it just displays the string as one line.

To preserve the line breaks in the string when printing on the screen, you will have to use the PHP nl2br() function.

The nl2br(), as its name suggest stands for "newline to break", ie. \n to <br>. It is an inbuilt PHP function used to insert HTML break tags in the place of all newlines in a string.

Example

<?php
$str = "My name is John.\r\nI am a developer.\r\nI like coding in PHP.";
echo nl2br($str); 

Output

My name is John.
I am a developer.
I like coding in PHP.