Multi-Line Strings and Text Editors

Share this article

PHP conveniently allows two main methods of using multi-line strings, where a string constant is broken over many lines. However, it can be hard to find a syntax highlighting text editor that will correctly highlight multi-line strings in PHP.

When I was switched text editors recently, support for multi-line strings when syntax highlighting was high on my list of required features.

I find multi-line strings convenient for writing SQL, as writing a non-trivial SQL query on one line does not make for very readable code.

Here are two examples of multi-line strings in PHP. The first is simple a quoted string split over multiple lines.

$sql = " SELECT product.productid, product.title FROM product WHERE product.type='boardgame' ";


This next example uses heredoc syntax, which allows you to specify a token to end the string with.

$sql = <<

The text editor I now use is Syn, which supports multi-line quoted strings but not heredoc syntax. Given the choice, I would rather use quoted strings than heredoc.

Here are all of the editors I evaluated.

Syn can highlight multi-line strings, but I found a couple of bugs in the latest version on their site, which is dated February 2004. Incidentally, if you email the developer, he'll be happy to give you a later build where the bugs are fixed. Alas, the fixes haven't made it into an official build.

I was very impressed with Notepad++ for its speed and responsiveness as well as for additional features such as code folding. Notepad++ does highlight multi-line strings correctly, but I couldn't find a way of showing a 'folder view' panel similar to that found in editors such as Syn, HTML-Kit, JEdit and Editplus. If anybody knows a way, please let me know.

TextPad does multi-line strings. What turned me off from this editor was that I couldn't find a folder view panel, and I couldn't specify bold or italic when setting up my syntax highlighting. The lack of bold and italic support is not as important to me.

JEdit does support multi-line strings in its syntax highlighting. It is a very impressive editor with a large range of plug-ins, but unfortunately I found it very slow to load. Responsiveness was one of my main criteria, and one of the reasons I chose the light-weight Syn for now.

UltraEdit looks great, but unfortunately does not have syntax highlighting for multi-line strings. According to their website, this will be supported in version 11.

Crimson editor is nice, but doesn't have support for multi-line strings. I don't know if this will be added in a future version.

HTML-Kit is a fantastic editor and used to be my editor of choice until relatively recently. While it supports heredoc syntax, it does not have support for regular multi-line strings in quotes. According to their FAQ, support will not be added and isn't available in a plugin due to architecture limitations. However, I'd definitely recommend this editor if you are not concerned with multi-line strings.

EditPlus has no support for multi-line strings, or for bold/italic when configuring syntax highlighting. Otherwise, it is very nice and has a folder view, and lots of nice extra features.

Why the need to support multi-line strings? It is just a personal preference of mine. There are alternative ways to achieve the same effect, however. For one, there is heredoc syntax, which is (to my surprise) supported by more editors that regular multi-line strings, though still not the majority. Other alternatives are workarounds such as the following:

$sql = "SELECT" . " product.productid," . " product.title" . "FROM" . " product" . "WHERE" . " product.type='boardgame'";

I found adding those dots and quotes to be highly frustrating, however, even just now! It would make that SQL hard to maintain in future.

How do you deal with multi-line strings in your code? Do you write SQL on one line, break it into multiple strings, or use multi-line strings?

Note: Initially when I compiled this list, I wrote that JEdit does not support multi-line strings. Upon testing it today, I see that it does. I've corrected my mistake.

Frequently Asked Questions (FAQs) about Multi-line Strings and Text Editors

What is a multi-line string and why is it important in programming?

A multi-line string is a sequence of characters that spans across multiple lines. It is a common feature in many programming languages like PHP, JavaScript, and Rust. Multi-line strings are important because they allow programmers to handle large amounts of text data efficiently. They are particularly useful when dealing with long strings that would be difficult to manage if they were written on a single line. This can include things like SQL queries, HTML templates, or any other large blocks of text that need to be embedded in your code.

How do I create a multi-line string in PHP?

In PHP, you can create a multi-line string using the heredoc or nowdoc syntax. Here’s an example of how to do it with heredoc:

$str = <<<EOD
Example of string
spanning multiple lines
using heredoc syntax.
EOD;

And here’s an example with nowdoc:

$str = <<<'EOD'
Example of string
spanning multiple lines
using nowdoc syntax.
EOD;

How do I create a multi-line string in JavaScript?

In JavaScript, you can create a multi-line string using the backtick (`) character. This is known as a template literal. Here’s an example:

let str = `Example of a
multi-line string
in JavaScript.`;

How do I create a multi-line string in Rust?

In Rust, you can create a multi-line string using the triple-quote (""") syntax. Here’s an example:

let str = r#"Example of a
multi-line string
in Rust."#;

What is string interpolation and how does it work with multi-line strings?

String interpolation is a process that substitutes values of variables into placeholders in a string. In languages like PHP, JavaScript, and Rust, you can use string interpolation with multi-line strings. This allows you to embed expressions or variables directly into your strings, which can be very useful when you need to generate dynamic text.

How do I handle indentation in multi-line strings?

Handling indentation in multi-line strings can be tricky, as different languages have different rules. In general, it’s best to avoid leading whitespace in your strings, as it can cause unexpected results. If you need to include indentation, consider using a function or method that strips out the unwanted whitespace.

Can I use special characters in multi-line strings?

Yes, you can use special characters in multi-line strings. However, how you escape these characters will depend on the language you’re using. In general, you can use a backslash (\) to escape special characters.

How do I concatenate multi-line strings?

Concatenating multi-line strings is similar to concatenating regular strings. You can use the + operator in languages like JavaScript, or the . operator in PHP. In Rust, you can use the format! macro to concatenate strings.

Can I use multi-line strings with functions and methods?

Yes, you can use multi-line strings with functions and methods. This can be useful when you need to pass a large block of text as an argument to a function or method.

Are there any performance considerations when using multi-line strings?

In general, using multi-line strings should not have a significant impact on performance. However, if you’re dealing with very large strings, it may be more efficient to use a buffer or a stream, rather than a multi-line string.

Thomas RutterThomas Rutter
View Author
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week