Double Quotes
A string enclosed with double quotes is evaluated by PHP. Variables and special character combinations are looked for and if found, interpreted. To have the double quote character inside of the string, precede it with a backslash (this is called “escaping”).
The backslash has a special roll in a double quoted string. It indicates a possible start of a combination of characters which need to be interpreted. Depending on the characters that follow, it and the characters will be interepreted and replaced by the interpreted value.
$var = "happy"; $text = "This is my text."; // This is my text. $text = "He said \"Hello\" to me."; // He said "Hello" to me. $text = "I am var today."; // I am var today. $text = "I am $var today."; // I am happy today. $text = "I am \$var today."; // I am $var today. // escaping the $ and not parsing the variable
Other special characters which will be interpreted:
| Character | Parsed As |
| \n | linefeed |
| \r | carriage return |
| \t | horizontal tab |
| \\ | backslash |
| \$ | dollar sign |
| (see below) | character in octal notation |
| (see below) | character in hexadecimal notation |
| since PHP 5.2.5 | |
| \v | vertical tab |
| \f | form feed |
Octal notation is a combination of one to three digets in the range of 1-7. [1, 2, or 3 numbers, each less than 8]
Hexidecimal notation is an “x” followed by one or two characters in the range of 0-9 and/or a-f (or A-F).
In regular expressions that’s:
Octal: [0-7]{1,3}
Hex: x[0-9A-Fa-f]{1,2}
$text = "Octal: \52"; // Octal: * $text = "Hex: \x23"; // Hex: #
As you can see, a backslash inside of a double quoted string could cause unwanted results. Be sure to escape it if you intend to have the backslash as part of the string. Any combination of characters not noted above will not be interpreted.
$var = "happy"; $text = "Backslash: \\$var"; // Backslash: \happy $text = "Nothing: \dog and \cat"; // Nothing: \dog and \cat
