This content originally appeared on Envato Tuts+ Tutorials and was authored by Monty Shokeen
It is not unusual that you will have to create multi-line strings when writing a program in PHP. There are many ways of creating a multi-line string in PHP. We will discuss three of them in detail in this tutorial.
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 multi-line 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 multi-line 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 \n
.
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.
<?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 multi-line string.
Using Heredoc and Nowdoc Syntax to Create Multi-line Strings
The third way to create multi-line 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 multi-line 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 multi-line 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 me 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.
Final Thoughts
We learned about three different ways of creating multi-line strings in PHP. Using PHP_EOL
to create multi-line strings helps you in avoiding hard-coded newline values while using Heredoc syntax makes sure you don't have to worry about escaping quotes.
This content originally appeared on Envato Tuts+ Tutorials and was authored by Monty Shokeen
Monty Shokeen | Sciencx (2022-05-07T02:41:05+00:00) How to Create a Multi-line String in PHP—3 Methods. Retrieved from https://www.scien.cx/2022/05/07/how-to-create-a-multi-line-string-in-php-3-methods/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.