String
Basics
Strings can be defined using one of
several methods. Most commonly, you will encapsulate them in single quotes or
double quotes. Unlike some other languages, these two methods behave quite
differently: single quotes represent “simple strings,” where almost all
characters are used literally. Double quotes, on the other hand, encapsulate
“complex strings” that allow for special escape sequences (for example, to insert
special characters) and for variable substitution, which makes it possible to embed
the value of a variable directly in a string, without the need for any special operator.
Escape sequences are sometimes
called control characters and take the form of a backslash (\) followed by one
or more characters. Perhaps the most common escape sequence is the newline
character \n. In the following example, we use hex and octal notation to
display an asterisk:
echo "\x2a";
echo "\052";
Variable
Interpolation
Variables can be embedded directly
inside a double-quote string by simply typing their name. For example:
$who =
"World";
echo "Hello
$who\n"; // Shows "Hello World" followed by a newline
echo ’Hello $who\n’;
// Shows "Hello $who\n"
Clearly, this “simple” syntax won’t
work in those situations in which the name of the variable you want to
interpolated is positioned in such a way inside the string that the parser
wouldn’t be able to parse its name in the way you intend it to. In these cases,
you can encapsulate the variable’s name in braces:
$me = ’Davey’;
$names = array
(’Smith’, ’Jones’, ’Jackson’);
echo "There
cannot be more than two {$me}s!";
echo "Citation:
{$names[1]}[1987]";
In the first example above, the
braces help us append a hard-coded letter “s” to the value of $me—without them,
the parser w oul d be looking for the variable $mes, which, obviously, does not
exist. In the second example, if the braces were not available, the parser
would interpret our input as $names[1][1987], which is clearly not what we intended.
Escaping
Literal Values
All three string-definition syntax
feature a set of several characters that require escaping in order to be
interpreted as literals.
When using single-quote strings,
single quote characters can be escaped using a backslash:
echo ’This is \’my\’
string’;
A similar set of escaping rules
apply to double-quote strings, where double quote characters and dollar sign
can also be escaped by prefixing them with a backslash:
$a = 10;
echo "The value
of \$a is \"$a\".";
Backslashes themselves can be
escaped in both cases using the same technique:
echo "Here’s an
escaped backslash: - \ -";
Note that you cannot escape a
brace—therefore, i f you need the literal string {$ to be printed out, you need
to escape the dollar sign in order to prevent the parser from interpreting the
sequence as an attempt to interpolate a variable:
echo "Here’s a
literal brace + dollar sign: {\$";
Heredoc strings provide the same
escaping mechanisms as double-quote strings, with the exception that you do not
need to escape double quote characters, since they have no semantic value.
0 Comment to " PHP : Strings and pattern - String basics "
Post a Comment