Friday, May 6, 2022

How to Create a Multi-line String in PHP—3 Methods

How to Create a Multi-line String in PHP—3 Methods

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:

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.

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.

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:

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.


No comments:

Post a Comment