PHP Tutorial – 05 – Strings
Combining strings
In PHP there are only two string operators. The first is the concatenation operator which combines two strings.
$a = "Hello"; $b = " World"; $c = $a . $b;
The second is the concatenating assignment operator that appends a string to the end of another string.
$a .= $b;
Delimiting strings
PHP strings can be delimited in four different ways. There are two single-line notations: double-quote and single-quote. The difference between them is that variables are not parsed in single-quoted strings whereas they are parsed in double-quoted strings. Single-quoted strings is prefered unless parsing is desired, because parsing strings has a small performance overhead.
echo "Hello $b"; // Hello World echo 'Hello $b'; // Hello $b
There are also two multi-line notations: heredoc and nowdoc. These notations are mainly used to include large blocks of text.
Heredoc strings
The heredoc syntax consists of the <<< operator followed by an identifier and a new line. The string is then included and after that the identifier is used again at the start of a new line in order to close the string. Variables are parsed inside of a heredoc string, just as with double-quoted strings.
$s = <<<LABEL Heredoc (with parsing) LABEL;
Nowdoc strings
The syntax for the nowdoc string is the same as for the heredoc string, except that the initial identifier is enclosed in single-quotes. No parsing is done inside a nowdoc string.
$s = <<<'LABEL' Nowdoc (without parsing) LABEL;
Escape characters
The escape character (\) is used to write special characters, such as newline or double-quote.
| Character | Meaning | Character | Meaning |
|---|---|---|---|
| \n | newline | \’ | single quote |
| \t | horizontal tab | \” | double quote |
| \$ | dollar sign | \\ | backslash |
When using the single-quote or nowdoc delimiter the only escape characters that work are for backslash (\\) and single-quote (\’) characters.
If you like this tutorial please +1 it:


![[Affiliate link] Total Training]( http://d3qzmfcxsyv953.cloudfront.net/images/pvt-affiliates/totaltraining.png)
![[Affiliate link] Lynda](http://d3qzmfcxsyv953.cloudfront.net/images/pvt-affiliates/lynda.png)

