| Tutorial PHP - Data TypesStrings on PHP Language Programing |
A string is a sequence of characters. A string can be delimited by single quotes or double quotes:
'PHP is cool'
"Hello, World!"
Double-quoted strings are subject to variable substitution and escape sequence handling, while single quotes are not. For example:
$a="World";
echo "Hello\t$a\n";
This displays "Hello" followed by a tab and then "World" followed by a newline. In other words, variable substitution is performed on the variable $a and the escape sequences are converted to their corresponding characters. Contrast that with:
echo 'Hello\t$a\n';
In this case, the output is exactly "Hello\t$a\n". There is no variable substitution or handling of escape sequences.
Another way to assign a string is to use what is known as the heredoc syntax. The advantage with this approach is that you do not need to escape quotes. It looks like this:
$foo = <<<EOD
This is a "multiline" string
assigned using the 'heredoc' syntax.
EOD;
The following table shows the escape sequences understood by PHP inside double-quoted strings.
|
Escape sequence |
Meaning |
|---|---|
|
\n |
Linefeed (LF or 0x0A (10) in ASCII) |
|
\r |
Carriage return (CR or 0x0D (13) in ASCII) |
|
\t |
Horizontal tab (HT or 0x09 (9) in ASCII) |
|
\\ |
Backslash |
|
\$ |
Dollar sign |
|
\" |
Double quote |
|
\123 |
Octal notation representation of a character |
|
\x12 |
Hexadecimal notation representation of a characte |
| Users' Comments (0) |
|
No comment posted








