When using the echo or print in PHP, sometimes the code needs to output characters verbatim, while other times the code needs to output the value of a variable or the value of an expression. Quoting is a syntactic mechanism for the PHP interpreter to differentiate whether something is to be printed verbatim.
In a context where text (also known as string) content is expected, a single quote begins a single quoted string. The quoted string ends when the PHP interpreter encounters the next unescaped single quote.
Unlike some other programming languages, the mechanism of escaping also works in single quoted strings. This is quite handy sometimes.
A double quoted string is much like a single quoted string, with the exception that it is enclosed by two unescaped double quote characters.
However, a double quoted string also has one flexibility that single quoted strings lack. Within a double quoted string, the specification of variables is replaced by the content of the specified variables. This means the use of double quoted strings can save a lot of concatenation operations.
Note that a double quoted string cannot print the value of an expression.
In a quoted string, the backslash character (∖) functions as an escape mechanism. “Escaping” means the ability to specify characters that otherwise cannot be specified.
For literal strings, the most common use of escaping is to enter quotes. For example, in a single quoted string, backslash single quote (∖’) produces a single quote as a part of the string, and it does not end the single quoted string.
Likewise, in a double quoted string, the sequence of backslash double quote (∖") specifies a double quote character as a part of the string.
Of course, a natural question is how to specify a backslash as itself in a quoted string. The answer is it escapes itself! In other words, backslash backslash (∖∖) produces a single backslash character as a part of the string.
Although HTML in general is line break insensitive (unless in a <pre> element, it helps with readability to insert actual line break characters for viewing HTML code as is without a browser rendering. A line break can be produced using the “newline” character. It is an escaped character, specified by backslash n (∖n).