1. Code
  2. Coding Fundamentals

How to Create a Multiline String in PHP: 3 Methods

Scroll to top
4 min read

It is not unusual that you will have to create multiline strings when writing a program in PHP. There are many ways of creating a multiline string in PHP. We will discuss three of them in detail in this tutorial.

1. Using Double Quotes and Escape Sequences

As you know, we can create strings in PHP using both single quotes and double quotes. We also have special escape sequences to output special characters. For example, we can use \n for a line feed and \r for a carriage return.

Writing \n or \r\n inside your strings will give you new lines in PHP. This allows you to easily create multiline characters. However, you have to keep in mind that these escape sequences only work in double-quoted strings. Trying to use them inside single quotes will output them exactly the way they are typed.

Here are some examples for creating multiline strings in PHP with double quotes:

1
<?php
2
3
$single_quote = 'How are you?\nI am fine.';
4
5
$double_quote = "How are you?\nI am fine.";
6
7
// How are you?\nI am fine.

8
echo $single_quote;
9
10
/*

11
How are you?

12
I am fine.

13
*/
14
echo $double_quote;
15
16
?>

One point that I would like to mention is that these newline character sequences are platform-specific. This means that you can run into some unexpected behavior by hard-coding these sequences in your code.

In general, Windows machines use \r\n, and Linux or macOS machines use \n.

2. Using PHP_EOL to Create Multiline Strings

You can avoid using hard-coded newline sequences in your code with the help of the PHP_EOL constant. This constant will automatically create the correct end of line symbol for the current platform on which the script is running.

1
<?php
2
3
$string_php_eol = "How are you?".PHP_EOL."I am fine.";
4
5
$php_eol_a = "How are you?".PHP_EOL;
6
$php_eol_b = "I am fine.".PHP_EOL;
7
$php_eol_c = "Want to go out tonight?".PHP_EOL;
8
$php_eol_d = "Yes, definitely.".PHP_EOL;
9
10
$joined_string_php_eol = $php_eol_a.$php_eol_b.$php_eol_c.$php_eol_d;
11
12
/*

13
How are you?

14
I am fine.

15
*/
16
echo $string_php_eol;
17
18
/*

19
How are you?

20
I am fine.

21
Want to go out tonight?

22
Yes, definitely. */
23
echo $joined_string_php_eol;
24
25
?>

For small multiline strings, you can concatenate everything on a single line. However, you can also spread your string across multiple variables before joining it to create your multiline string.

3. Using Heredoc and Nowdoc Syntax to Create Multiline Strings

The third way to create multiline strings in PHP involves the use of Heredoc and Nowdoc syntax.

The Heredoc syntax requires us to provide an identifier and a new line after the <<< operator. After that, we write our own string and again use the same identifier to mark the end of our multiline string.

Starting from PHP 7.3.0, you can add indentation before the closing identifier. However, keep in mind that the indentation of the end identifier does not go beyond the indentation of any lines in the multiline string. Also make sure that the indentation does not have a mix of spaces and tabs.

1
<?php
2
3
$name = "Monty";
4
5
$heredoc_a = <<<HEREA
6
    How are you, $name?
7
    I am fine.
8
    What to go out tonight?
9
    Yes, definitely.
10
HEREA;
11
12
$heredoc_b = <<<HEREB
13
    How are you, $name?
14
    I am fine.
15
    What to go out tonight?
16
    Yes, definitely.
17
    HEREB;
18
19
/*

20
    How are you, Monty?

21
    I am fine.

22
    What to go out tonight?

23
    Yes, definitely.

24
*/
25
echo $heredoc_a;
26
27
/*

28
How are you, Monty?

29
I am fine.

30
What to go out tonight?

31
Yes, definitely.

32
*/
33
echo $heredoc_b;
34
35
?>

As you can see in the above example, text within Heredoc syntax behaves as if it is within double quotes. What if you want something that emulates the behavior of single-quote strings? This is where you will find the Nowdoc syntax useful.

The differentiating factor between Heredoc and Nowdoc is that the latter requires you to wrap its identifier within single quotes. The following example should make it clear:

1
<?php
2
3
$name = "Monty";
4
5
$nowdoc_a = <<<'HEREA'
6
    How are you, $name?
7
    I am fine.
8
    What to go out tonight?
9
    Yes, definitely.
10
HEREA;
11
12
$nowdoc_b = <<<'HEREB'
13
    How are you, $name?
14
    I am fine.
15
    What to go out tonight?
16
    Yes, definitely.
17
    HEREB;
18
19
/*

20
    How are you, $name?

21
    I am fine.

22
    What to go out tonight?

23
    Yes, definitely.

24
*/
25
echo $nowdoc_a;
26
27
/*

28
How are you, $name?

29
I am fine.

30
What to go out tonight?

31
Yes, definitely.

32
*/
33
echo $nowdoc_b;
34
35
?>

As you can see, the variable $name was not substituted with its actual value when used within Nowdoc syntax.

Final Thoughts

We learned about three different ways of creating multiline strings in PHP. Using PHP_EOL to create multiline strings helps you in avoiding hard-coded newline values, while using Heredoc syntax makes sure you don't have to worry about escaping quotes.

Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.