Why Using Here-Strings?

by Jan 15, 2013

To create multi-line text in a script, you can simply enter the multi-line text into your script using quotes:

$text = 'First Line
Second Line
Third Line'
$text

Why should you ever bother using the so-called here-strings?

$text = @'
First Line
Second Line
Third Line
'@
$text

Here-strings have a strict format design: immediately after @' there has to be a new line, and the closing term '@ must start at the beginning of a new line. Because of this, anything in between is treated as content, no matter what characters you use.

Here-strings mask special characters like quotes or hashes. Simple quotes would interpret them, for example as end-of-string or comment, respectively. That's why here-strings are a safe way of enclosing source code or XML data.

Twitter This Tip! ReTweet this Tip!