Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
A string is a series of characters, typically specified within quotations. Explore the differences between single and double quoted strings and using other variables within a string.
Documentation
Learn more about Strings
Single Quoted Strings are the simplest way to specify a string. Enclose the string in single quotes (the character ').
To specify a literal single quote, escape it with a backslash (\'). To specify a literal backslash, double it (\\). All other instances of backslash will be treated as a literal backslash: this means that the other escape sequences you might be used to, such as \r or \n, will be output literally as specified rather than having any special meaning.
Note: variables and escape sequences for special characters will not be expanded when they occur in single quoted strings.
Double Quoted Strings are enclosed in double-quotes ("), PHP will interpret more escape sequences for special characters. As in single quoted strings, escaping any other character will result in the backslash being printed too.
The most important feature of double-quoted strings is the fact that variable names will be expanded.
Heredoc Strings
Although quotes are the primary way to specify a string, there is another option: the heredoc syntax (<<<). After this operator, an identifier is provided, then a newline. The string itself follows, and then the same identifier again to close the quotation.
The closing identifier must begin in the first column of the line. Also, the identifier must follow the same naming rules as any other label in PHP: it must contain only alphanumeric characters and underscores, and must start with a non-digit character or underscore.
Heredoc syntax allows people to easily write large amounts of text from within PHP, but without the need to constantly escape things. Put simply, it allows you to define your own string limiter so that you can make it something other than a double or single quote. So, for example, we could use the string "EOT" (end of text) for our delimiter, meaning that we can use double quotes and single quotes freely within the body of the text - the string only ends when we type EOT.
Example
$name = 'Alena';
$myString = <<<EOT
Example of string
spanning multiple lines
using spaces
and including a $name variable
using heredoc syntax.
EOT;
Would output:
Example of string
spanning multiple lines
using spaces
and including a Alena variable
using heredoc syntax.
Nowdoc Strings
Nowdocs are to single-quoted strings what heredocs are to double-quoted strings. A nowdoc is specified similarly to a heredoc, but no parsing is done inside a nowdoc. The construct is ideal for embedding PHP code or other large blocks of text without the need for escaping.
A nowdoc is identified with the same <<< sequence used for heredocs, but the identifier which follows is enclosed in single quotes, e.g. <<<'EOT'. All the rules for heredoc identifiers also apply to nowdoc identifiers, especially those regarding the appearance of the closing identifier.
Example
$name = 'Alena';
$str = <<<'EOT'
Example of string
spanning multiple lines
using spaces
and including a $name variable
using nowdoc syntax.
EOT;
Would output:
Example of string
spanning multiple lines
using spaces
and including a $name variable
using nowdoc syntax.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up