# String: Syntax

A string literal can be specified in four different ways.

### Single quoted

<?php
echo 'this is a simple string';

echo 'You can also have embedded newlines in
strings this way as it is
okay to do';

// Outputs: Arnold once said: "I'll be back"
echo 'Arnold once said: "I\'ll be back"';

// Outputs: You deleted C:\*.*?
echo 'You deleted C:\\*.*?';

// Outputs: You deleted C:\*.*?
echo 'You deleted C:\*.*?';

// Outputs: This will not expand: \n a newline
echo 'This will not expand: \n a newline';

// Outputs: Variables do not $expand$either
echo 'Variables do not $expand$either';
?>

The simplest way to specify a string is to enclose it in single quotes (the character ').

Unlike the double-quoted and heredoc syntaxes, variables and escape sequences for special characters will not be expanded when they occur in single quoted strings.

To specify a literal single quote, escape it with a backslash (\).
To specify a literal backslash, double it (\\).
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.

### Double quoted

If the string is enclosed in double-quotes ("), PHP will interpret the following escape sequences for special characters.

The most important feature of double-quoted strings is the fact that variable names will be expanded.

### Heredoc syntax

<?php
$str = <<<EOD Example of string spanning multiple lines using heredoc syntax. EOD; /* More complex example, with variables. */ class foo { var$foo;
var $bar; function __construct() {$this->foo = 'Foo';
$this->bar = array('Bar1', 'Bar2', 'Bar3'); } }$foo = new foo();
$name = 'MyName'; echo <<<EOT My name is "$name". I am printing some $foo->foo. Now, I am printing some {$foo->bar[1]}.
This should print a capital 'A': \x41
EOT;
?>
<?php
// Static variables
function foo()
{
static $bar = <<<LABEL Nothing in here... LABEL; } // Class properties/constants class foo { const BAR = <<<FOOBAR Constant example FOOBAR; public$baz = <<<FOOBAR
Property example
FOOBAR;
}
?>
<?php
echo <<<"FOOBAR"
Hello World!
FOOBAR;
?>
• A third way to delimit strings is 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.
• Heredoc text behaves just like a double-quoted string, without the double quotes.
• Quotes in a heredoc do not need to be escaped.
• Variables are expanded, but the same care must be taken when expressing complex variables inside a heredoc as with strings.
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.

### Nowdoc syntax

<?php
echo <<<'EOD'
Example of string spanning multiple lines
using nowdoc syntax. Backslashes are always treated literally,
e.g. \\ and \'.
EOD;
<?php
class foo
{
public $foo; public$bar;

function __construct()
{
$this->foo = 'Foo';$this->bar = array('Bar1', 'Bar2', 'Bar3');
}
}

$foo = new foo();$name = 'MyName';

echo <<<'EOT'
My name is "$name". I am printing some$foo->foo.
Now, I am printing some {$foo->bar[1]}. This should not print a capital 'A': \x41 EOT; ?> <?php class foo { public$bar = <<<'EOT'
bar
EOT;
}
?>
• 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.
• It shares some features in common with the SGML &lt;![CDATA[ ]]&gt; construct, in that it declares a block of text which is not for parsing.
• 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.

Syntax

## String: Syntax — Structure map

Clickable & Draggable!