Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
  1. Code
  2. PHP

How to Create a Multiline String in PHP: 3 Methods

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:

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.

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.

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:

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?
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.