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.
\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:
<?php $single_quote = 'How are you?\nI am fine.'; $double_quote = "How are you?\nI am fine."; // How are you?\nI am fine. echo $single_quote; /* How are you? I am fine. */ echo $double_quote; ?>
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
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.
<?php $string_php_eol = "How are you?".PHP_EOL."I am fine."; $php_eol_a = "How are you?".PHP_EOL; $php_eol_b = "I am fine.".PHP_EOL; $php_eol_c = "Want to go out tonight?".PHP_EOL; $php_eol_d = "Yes, definitely.".PHP_EOL; $joined_string_php_eol = $php_eol_a.$php_eol_b.$php_eol_c.$php_eol_d; /* How are you? I am fine. */ echo $string_php_eol; /* How are you? I am fine. Want to go out tonight? Yes, definitely. */ echo $joined_string_php_eol; ?>
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.
<?php $name = "Monty"; $heredoc_a = <<<HEREA How are you, $name? I am fine. What to go out tonight? Yes, definitely. HEREA; $heredoc_b = <<<HEREB How are you, $name? I am fine. What to go out tonight? Yes, definitely. HEREB; /* How are you, Monty? I am fine. What to go out tonight? Yes, definitely. */ echo $heredoc_a; /* How are you, Monty? I am fine. What to go out tonight? Yes, definitely. */ echo $heredoc_b; ?>
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:
<?php $name = "Monty"; $nowdoc_a = <<<'HEREA' How are you, $name? I am fine. What to go out tonight? Yes, definitely. HEREA; $nowdoc_b = <<<'HEREB' How are you, $name? I am fine. What to go out tonight? Yes, definitely. HEREB; /* How are you, $name? I am fine. What to go out tonight? Yes, definitely. */ echo $nowdoc_a; /* How are you, $name? I am fine. What to go out tonight? Yes, definitely. */ echo $nowdoc_b; ?>
As you can see, the variable
$name was not substituted with its actual value when used within Nowdoc syntax.
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.
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.Update me weekly