Most data you encounter as you program will be sequences of characters, or strings. Strings can hold people’s names, passwords, addresses, credit card numbers, links to photographs, purchase histories, and more. For that reason, PHP has an extensive selection of functions for working with strings.
This chapter shows the many ways to create strings in your programs, including the sometimes tricky subject of interpolation (placing a variable’s value into a string), then covers functions for changing, quoting, manipulating, and searching strings. By the end of this chapter, you’ll be a string-handling expert.
There are four ways to write a literal string in your PHP code: using single quotes, double quotes, the here document (heredoc) format derived from the Unix shell, and its “cousin” now document (nowdoc). These methods differ in whether they recognize special escape sequences that let you encode other characters or interpolate variables.
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 their contained values. 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 in a 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 any 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 and nowdocs do not interpolate variables. Thus, the variable name in the following string is not expanded because the string literal in which it occurs is single-quoted:
$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 = ' '; // not an escape echo $nope; Tim O'Reilly C:WINDOWS
Double-quoted strings interpolate variables and expand the many PHP escape sequences. Table 4-1 lists the escape sequences recognized by PHP in double-quoted strings.
Escape sequence | Character represented |
---|---|
" |
Double quotes |
|
Newline |
|
Carriage return |
|
Tab |
\ |
Backslash |
$ |
Dollar sign |
{ |
Left curly brace |
} |
Right curly brace |
[ |
Left square bracket |
] |
Right square bracket |
|