Up to Speed with PHP: Choosing your Quotation Marks Carefully


In PHP we typically see two ways of representing a literal string value, one using single quotation marks
$str = 'this is a string';
and another using double quotation marks.
$str = "this is a string";
And the first thing to note is that if you use single quotation marks within a string value that opens and closes with single quotation marks then you must escape these,
$str = 'this is a \'string\'';
whereas if you use double quotation marks within single quotation marks then you do not escape them,
$str = 'this is a "string"';
and vice versa when using double quotation marks to enclose a string.
$str = "this is a \"string\"";
$str = "this is a 'string'";
But the differences between single and double quotation marks are more than skin deep. The main difference is that single quotations interpret most things literally
$value = 'Hello PHP!';
$str  = '$value';
echo $str; // outputs: $value
whereas double quotation marks interpret the variables:
$value = 'Hello PHP!';
$str  = "$value";
echo $str; // outputs: Hello PHP!
Strings contained within double quotation marks go beyond simply interpreting variables and contain some regular expression support as well. One important thing to note however is that if you wish to isolate variables to avoid them going unrecognized then you must use curly braces. For example:
$value = 'bellow';
$str = "{$value}ing";
echo $str;

Concatenation

Placing variables directly into strings (string interpolation) isn't the only way to include them. As with other languages there's also concatenation.
$str = 'hello';

$str2 = $str . $str;
echo $str; // hello
echo $str2; // hellohello

$str .= $str;
echo $str; // hellohello
And this works for strings that are created using single or double quotes.

Nowdoc and heredoc

Once we know the difference between single and double quotation marks then we know which is appropriate for any given situation, but there is an extension of their functionality in the nowdoc and heredoc syntaxes.

Nowdoc

Nowdoc can be thought of as equivalent to using single quotation marks but with one important difference, that there is no need to escape single quotation marks. This leads to a cleaner approach for long strings:
<<<'ANYTEXTHERE'
This is a longer passage with 'quoted material'. There is no requirement to escape 'single' or "double" quotation marks. It starts with three less than signs followed by a word in quotation marks, followed by a line return, and ends with a line that contains only that single same word (without quotation marks) and a semi-colon.
ANYTEXTHERE;
A nowdoc string begins with three less-than signs (or opening angle brackets) followed by an identifier in single quotes and a line return. This marks the beginning of the string and also indicates how the string will end: the same identifier as that which opens the string closes it by featuring on its own line without quotation marks. This identifier can be absolutely anything and in upper or lowercase, the only requirements are that,
it must contain only alphanumeric characters and underscores, and must start with a non-digit character or underscore.

Heredoc 

$value = 'Hello PHP!';

$str = <<<ANYTEXTHERE
$value This is a longer passage with 'quoted material'. There is no requirement to escape 'single' or "double" quotation marks. It starts with three less than signs followed by a word in quotation marks and ends with a line that contains only that single same word and a semi-colon.
ANYTEXTHERE;
Heredoc syntax is exactly the same as nowdoc, except the opening identifier does not have quotation marks of any kind. The behaviour, meanwhile, is the same as using double quotes without the need to escape double quotes contained in strings.

To learn more about Strings of all kinds in PHP visit the documentation pages.



Endorse on Coderwall

Comments