Wednesday, July 3, 2013

There are three ways to write a literal string in your program: using single quotes, double
quotes, and the here document (heredoc) format derived from the Unix shell. These
methods differ in whether they recognize special escape sequences that let you encode
other characters or interpolate variables.

Variable Interpolation

When you define a string literal using double quotes or a heredoc, the string is subject
to variable interpolation. Interpolation is the process of replacing variable names in the
string with the values of those variables. There are two ways to interpolate variables
into strings.
The simpler of the two ways is to put the variable name in a double-quoted string or
heredoc:
$who = 'Kilroy';
$where = 'here';
echo "$who was $where";
Kilroy was here
The other way is to surround the variable being interpolated with curly braces. Using
this syntax ensures the correct variable is interpolated. The classic use of curly braces
is to disambiguate the variable name from surrounding text:
$n = 12;
echo "You are the {$n}th person";
You are the 12th person
Without the curly braces, PHP would try to print the value of the $nth variable.
Unlike in some shell environments, in PHP strings are not repeatedly processed for
interpolation. Instead, any interpolations in a double-quoted string are processed first
and the result is used as the value of the string:
$bar = 'this is not printed';
$foo = '$bar'; // single quotes
print("$foo");
$bar

Single-Quoted Strings

Single-quoted strings do not interpolate variables. Thus, the variable name in the following
string is not expanded because the string literal in which it occurs is singlequoted:
$name = 'Fred';
$str = 'Hello, $name'; // single-quoted
echo $str;
Hello, $name
The only escape sequences that work in single-quoted strings are \', which puts a single
quote in a single-quoted string, and \\, which puts a backslash in a single-quoted string.
Any other occurrence of a backslash is interpreted simply as a backslash:
$name = 'Tim O\'Reilly';// escaped single quote
echo $name;
$path = 'C:\\WINDOWS'; // escaped backslash
echo $path;
$nope = '\n'; // not an escape
echo $nope;
Tim O'Reilly
C:\WINDOWS
\n

Double-Quoted Strings

Double-quoted strings interpolate variables and expand the many PHP escape sequences.
Table 1 lists the escape sequences recognized by PHP in double-quoted strings.

Table 1: Escape sequences in double-quoted strings

If an unknown escape sequence (i.e., a backslash followed by a character that is not
one of those in Table 1) is found in a double-quoted string literal, it is ignored (if you
have the warning level E_NOTICE set, a warning is generated for such unknown escape
sequences):
$str = "What is \c this?";// unknown escape sequence
echo $str;
What is \c this?

Here Documents

You can easily put multiline strings into your program with a heredoc, as follows:
$clerihew = <<< EndOfQuote
Sir Humphrey Davy
Abominated gravy.
He lived in the odium
Of having discovered sodium.
EndOfQuote;
echo $clerihew;
Sir Humphrey Davy
Abominated gravy.
He lived in the odium
Of having discovered sodium.
The <<< identifier token tells the PHP parser that you’re writing a heredoc. There
must be a space after the <<< and before the identifier. You get to pick the identifier.
The next line starts the text being quoted by the heredoc, which continues until it
reaches a line that consists of nothing but the identifier.
As a special case, you can put a semicolon after the terminating identifier to end the
statement, as shown in the previous code. If you are using a heredoc in a more complex
expression, you need to continue the expression on the next line, as shown here:
printf(<<< Template
%s is %d years old.
Template
, "Fred", 35);
Single and double quotes in a heredoc are passed through:
$dialogue = <<< NoMore
"It's not going to happen!" she fumed.
He raised an eyebrow. "Want to bet?"
NoMore;
echo $dialogue;
"It's not going to happen!" she fumed.
He raised an eyebrow. "Want to bet?"
Whitespace in a heredoc is also preserved:
$ws = <<< Enough
boo
hoo
Enough;
// $ws = " boo\n hoo";
The newline before the trailing terminator is removed, so these two assignments are
identical:
$s = 'Foo';
// same as
$s = <<< EndOfPointlessHeredoc
Foo
EndOfPointlessHeredoc;
If you want a newline to end your heredoc-quoted string, you’ll need to add an extra
one yourself:
$s = <<< End
Foo
End;

0 comments:

Post a Comment