The Building Blocks / Page 2 | WebReference

The Building Blocks / Page 2

[previous] [next]

The Building Blocks:
Data Types, Literals, Variables, and Constants

String Literals and Quoting

We introduce strings in this chapter but Chapter 6, "Strings," provides a more comprehensive coverage. String literals are a row of characters enclosed in either double or single quotes.[1] The quotes must be matched. If the string starts with a single quote, it must end with a matching single quote; likewise if it starts with a double quote, it must end with a double quote. If a string of characters is enclosed in single quotes, the characters are treated literally (each of the characters represents itself). We can say the single quotes are the democratic quotes: All characters are treated equally.

Double quotes do not treat all characters equally. If a string is enclosed in double quotes, most of the characters represent themselves, but dollar signs and backslashes have a special meaning as shown in the following examples.

Single quotes can hide double quotes, and double quotes can hide single quotes:[2]

1. PHP always null-terminates strings internally and keeps track of the length of the string.

2. PHP recognizes editors that use straight quotes, such as vi or Notepad, but not editors that automatically transform straight quotes into curly quotes.

An empty set of quotes is called the null string. If a number is enclosed in quotes, it is considered a string; for example, "5" is a string, whereas 5 is a number.

Strings are called constants or literals. The string value "hello" is called a string constant or literal. To change a string requires replacing it with another string.

Strings can contain escape sequences (a single character preceded with a backslash). Escape sequences cause a character to behave in a certain way; for example, a "\t" represents a tab and "\n" represents a newline. The backslash is also used for quoting a single character so that it will not be interpreted; for example, \$5.00 where the dollar sign in PHP is used to represent variables rather than money. \$5.00 could also be written as '$5' because single quotes protect all characters from interpretation.

Here documents, also called here-docs, provide a way to create a block of text that simplifies writing strings containing lots of single quotes, double quotes, and variables (see Example 4.4).

Example 4.3


  1. PHP program starts here.
  2. $name is a PHP variable. It is assigned the string "Nancy". You will learn all about variables in the section "Variables" on page 70.
  3. When a string is enclosed within double quotes, the PHP interpreter will substitute the variable with its value; for example, $name will be replaced with "Nancy".
  4. When a string is enclosed in single quotes, all characters are treated as literals. Variable substitution will not occur.
  5. Single quotes can be nested within double quotes and vice versa.
  6. Quotes can be escaped with a backslash to make them literal characters within a string.
  7. The dollar sign is escaped from PHP interpretation, that is, is treated as a literal character.
  8. A string in double quotes is concatenated to a string in single quotes. Just as the backslash protects the dollar sign from interpretation, so do the single quotes. Remember, characters in single quotes are all treated as literals; that is, PHP does not consider any of the enclosed characters as special. See the output in Figure 4.4.

Figure 4.4 Single and double quotes.

[previous] [next]